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