how to initialize char *

This is a discussion on how to initialize char * within the C Programming forums, part of the General Programming Boards category; i hav a program structure which is similar to the code snippet given below.I don't know how to initialize the ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    3

    how to initialize char *

    i hav a program structure which is similar to the code snippet given below.I don't know how to initialize the char * .plz help me in this .Also tell me where should i initialize the char * that is declared inside the structure.

    Code:
    #include<stdio.h>
    #define MAX 10
    struct res_list
    {
    int res_type;
    char *res_name;//is it necessary that i should initialize *res_name??
    char *res_loc;//is it necessary that i should initialize *res_loc??
    int res_pri;
    }res[MAX];
    
    main()
    {
    int g_id,i=0,nor,j;
    char *g_name,n;
    g_name=malloc(25);// i get error in this line.Is there any othre way to initilaize *g_name??
    
    printf("\nEnter the Following Data...\n");
    printf("Enter g_id:");
    scanf("%d",&g_id);
    printf("\nEnter g_name:");
    scanf("%s",&g_name);
    printf("\nEnter no. of resources:");
    scanf("%d",&n);
    do
    {
    printf("\nEnter %d res_type:",i+1);
    scanf("%d",&res[i].res_type);
    
    printf("\nEnter %d res_name:",i+1);
    scanf("%s",&res[i].res_name);
    
    printf("\nEnter %d res_location:",i+1);
    scanf("%s",&res[i].res_loc);
    
    printf("\nEnter %d res_priority:",i+1);
    scanf("%d",&res[i].res_pri);
    
    printf("Do u want to add anoter resource (Y/N)?");
    //scanf("%c",n);     i get segmentation fault if this statement is not commented
    i++;
    }while(n=='Y'||n=='y');
    nor=i-1;
    j=0;
    printf("\n\nG_id:%d",g_id);
    printf("\nG_Name:%s",g_name);
    
    while(j<nor)
    {
    printf("\nResource %d\n",j+1);
    printf("\nRes_Type:%d:",res[j].res_type);
    printf("\nRes_Name:%s:",res[j].res_name);
    printf("\nRes_Location:%s:",res[j].res_loc);
    printf("\nRes_Priority:%d:",res[j].res_pri);
    j++;
    }
    
    }
    
    
    
    this is the error msg:
    
    store.c: In function ‘main’:
    store.c:15: warning: incompatible implicit declaration of built-in function ‘malloc’
    plz help me in this
    Last edited by vignesh; 03-30-2009 at 01:05 AM.

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,456
    for malloc you need to include stdlib.h

    also - your indentation is missing - work on it
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    3
    thank u very much for ur reply.can u plz tel me...how to wite this in pointer version??

    Code:
    struct res_list
    {
    int res_type;
    char res_name[20];
    int res_pri;
    }res[MAX];
    
    
    i tried this... but i am getting error msg while getting input for that
    struct res_list
    {
    int res_type;
    char *res_name;  //where should i initialize *res_name??
    int res_pri;
    }res[MAX];

  4. #4
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,456
    Quote Originally Posted by vignesh View Post
    i tried this... but i am getting error msg while getting input for that
    char *res_name; //where should i initialize *res_name??
    Somewhere before you are using it
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 10-29-2006, 04:04 AM
  2. Need help understanding info in a header file
    By hicpics in forum C Programming
    Replies: 8
    Last Post: 12-02-2005, 11:36 AM
  3. Wierd Segmentation Faults on Global Variable
    By cbranje in forum C Programming
    Replies: 6
    Last Post: 02-19-2005, 11:25 AM
  4. comparing fields in a text file
    By darfader in forum C Programming
    Replies: 9
    Last Post: 08-22-2003, 08:21 AM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21