Name: Anonymous 2007-03-24 21:50 ID:Hy0hWHCa
hello 4chan,
say i have a structure:
struct X{
char *a;
char *b;
char *c;
};
and I also have an array of strings:
char *str[]={"hey","ho","yeps"};
Is there a way that I can dynamically allocate the members of X to the size of each corresponding string in the array, using a loop? I don't want to do the following, since my structure has more elements, it just seems downright inefficient.
X.a=malloc(sizeof(char)*strlen(str[0])+1);
X.b=malloc(sizeof(char)*strlen(str[1])+1);
X.c=malloc(sizeof(char)*strlen(str[2])+1);
It looks like I just need a way to index each member...I don't know if that's possible, so if not, how else would i accomplish this?
say i have a structure:
struct X{
char *a;
char *b;
char *c;
};
and I also have an array of strings:
char *str[]={"hey","ho","yeps"};
Is there a way that I can dynamically allocate the members of X to the size of each corresponding string in the array, using a loop? I don't want to do the following, since my structure has more elements, it just seems downright inefficient.
X.a=malloc(sizeof(char)*strlen(str[0])+1);
X.b=malloc(sizeof(char)*strlen(str[1])+1);
X.c=malloc(sizeof(char)*strlen(str[2])+1);
It looks like I just need a way to index each member...I don't know if that's possible, so if not, how else would i accomplish this?