Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

help with c structures

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?

Name: Anonymous 2007-03-25 14:01 ID:HQzfGGQb

>>5
Unless you're in a 64-bit environment, in which case pointers are 64 bits long. (On IBM's virtual whatchamacallit architecture, pointers are 128 bits long. Whoop!)

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List