When i tried to make the structure members(res_name,res_loc) as char pointer, i am getting error. i don't know how to convert res_name[25] to pointer.plz help me in this

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX 10

struct resource
{
int res_type;
char res_name[25]; // i want to use this as char * res_name
char res_loc[30];   // if i give it in that way where should i initialize it and how to             
                                //access it inside the function.plz tell me
int res_pri;
}res[MAX];
int store(int g_id,char *g_name,struct resource res[],int n);
main()
{
int g_id,i=0,choice,nor,j,status;
char *g_name;
g_name=(char *)malloc(sizeof(char) * (MAX+1));

printf("\nEnter the Following Data...\n");
printf("Enter g_id:");
scanf("%d",&g_id);
printf("\nEnter g_name:");
scanf("%s",g_name);
printf("\nEnter the resources...");

do
{
printf("\nEnter %d res_type:",i+1);
scanf("%d",&res[i].res_type);

printf("Enter %d res_name:",i+1);
scanf("%s",res[i].res_name);

printf("Enter %d res_location:",i+1);
scanf("%s",res[i].res_loc);

printf("Enter %d res_priority:",i+1);
scanf("%d",&res[i].res_pri);


printf("\nDo u want to add another resource?\n 1.Yes\n2.N0\n:");
scanf("%d",&choice);

i++;
}while(choice==1);
nor=i-1;
j=0;
printf("--------------------------------------------------------------------------------------------------");
printf("\n\nG_id:%d",g_id);
printf("\nG_Name:%s",g_name);

status=store(g_id,g_name,res,nor);


}


struct 
{
int g_id;
char g_name[25];
struct resource res[];
}g;

void store(int g_id,char *g_name,struct resource res[],int n)
{
int i=0,j=0;
g.g_id=g_id;
strcpy(g.g_name,g_name);
while(i<=n)
{
g.res[i].res_type=res[i].res_type;
strcpy(g.res[i].res_name,res[i].res_name);
strcpy(g.res[i].res_loc,res[i].res_loc);
g.res[i].res_pri=res[i].res_pri;
i++;
}

printf("\nG.G_id:%d",g.g_id);
printf("\nG.G_Name:%s",g.g_name);
while(j<=n)
{
fprintf(fp,"\nResource %d\n",j+1);
printf("**************");
printf("\ngr.Res_Type:%d:",g.res[j].res_type);
printf("\ngr.Res_Name:%s",g.res[j].res_name);
printf("\ngr.Res_Location:%s",g.res[j].res_loc);
printf("\ngr.Res_Priority:%d:\n",g.res[j].res_pri);
j++;
}

just do this: char* res_name . But, the trick is that space needs to be allocated before it can be used the first time, such as res_name = malloc(25); initialize it in the do loop

do
{
printf("\nEnter %d res_type:",i+1);
res[i].res_type = malloc(25);
scanf("%d",&res[i].res_type);

just do this: char* res_name . But, the trick is that space needs to be allocated before it can be used the first time, such as res_name = malloc(25); initialize it in the do loop

do
{
printf("\nEnter %d res_type:",i+1);
res[i].res_type = malloc(25);
scanf("%d",&res[i].res_type);

I am getting segmentation fault if i try out in ur way.Anyother option is there???plz tel me

Post your program, just repeating what I posted is useless.

[edit]Ohh I think I see it -- remove that & sysbol in front of &res... scanf("%d",res[i].res_type); [/edit]

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX 10

struct resource
{
char *res_type;
char *res_name;
char *res_loc;
int res_pri;
}res[MAX];

int store(int g_id,char *g_name,struct resource res[],int n);
main()
{
int g_id,i=0,choice,nor,j,status;
char *g_name;
g_name=(char *)malloc(sizeof(char) * (MAX+1));

printf("\nEnter the Following Data...\n");
printf("Enter g_id:");
scanf("%d",&g_id);
printf("\nEnter g_name:");
scanf("%s",g_name);
printf("\nEnter the resources...");

do
{
res[i].res_type = (char *)malloc(sizeof(char) * (30));
printf("\nEnter %d res_type:",i+1);
scanf("%s",res[i].res_type);

res[i].res_name = (char *)malloc(sizeof(char) * (15));
printf("Enter %d res_name:",i+1);
scanf("%s",res[i].res_name);

res[i].res_loc = (char *)malloc(sizeof(char) * (30));
printf("Enter %d res_location:",i+1);
scanf("%s",res[i].res_loc);

printf("Enter %d res_priority:",i+1);
scanf("%d",&res[i].res_pri);


printf("\nDo u want to add another resource?\n 1.Yes\n2.N0\n:");
scanf("%d",&choice);

i++;
}while(choice==1);
nor=i-1;
j=0;

printf("\n\nG_id:%d",g_id);
printf("\nG_Name:%s",g_name);

status=store(g_id,g_name,res,nor);
if(status==0)
printf("\nConfig file updated successfully...\n");
else
printf("\nConfig file update failed...\n");

}


struct 
{
int g_id;
char g_name[25];
struct resource res[];
}g;

int store(int g_id,char *g_name,struct resource res[],int n)
{
FILE *fp;
int i=0,j=0;

fp=fopen("config.txt","a");
if (fp==NULL)
{
printf("Cannot open config file");
return 1;
}
else
{

g.g_id=g_id;
strcpy(g.g_name,g_name);

while(i<=n)
{
g.res[i].res_type=res[i].res_type;
strcpy(g.res[i].res_name,res[i].res_name);
strcpy(g.res[i].res_loc,res[i].res_loc);
g.res[i].res_pri=res[i].res_pri;
i++;
}

fprintf(fp,"\nG.G_id:%d",g.g_id);
fprintf(fp,"\nG.G_Name:%s",g.g_name);

while(j<=n)
{
fprintf(fp,"\nResource %d\n",j+1);
fprintf(fp,"**************");
fprintf(fp,"\ngr.Res_Type:%s:",g.res[j].res_type);
fprintf(fp,"\ngr.Res_Name:%s",g.res[j].res_name);
fprintf(fp,"\ngr.Res_Location:%s",g.res[j].res_loc);
fprintf(fp,"\ngr.Res_Priority:%d:\n",g.res[j].res_pri);
j++;
}
fclose(fp);
return 0;
}


}

this is the code

now i changed the res_type to char instead of integer.And i removed & symbol before itself.But still its not working.I don't know what mistake i have done.

I don't know wat mistake i have done.It prevents me proceeding further.Plz help me out.

I compiled (VC++ 2008 Express) and ran your program -- worked for me. Please post the data that you entered for each question. Maybe its a problem with your data entries.

This is the output when i use the array version.......

Enter the Following Data...

Enter Node IP Address:123.122.21.11
Enter g_id:12
Enter g_name:vignesh
Enter the resources...
Enter 1 res_type:hardware
Enter 1 res_name:cd-drive
Enter 1 res_location:xyz
Enter 1 res_priority:2

Do u want to add another resource?
 1.Yes
2.N0
:2
--------------------------------------------------------------------------------------------------

G_id:12
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.