Indefinite-length array as member of struct: how?
Randy Ellis
ellis at fozzy.UUCP
Tue Jul 18 02:54:44 AEST 1989
In article <7360 at c3pe.UUCP>, charles at c3pe.UUCP (Charles Green) writes:
> I have an application where I'm building and manipulating a stack of
> variable-length strings. I've set up a linked list of nodes, each one
> declared as follows:
> When I know how long the string is I'm pushing onto the stack, I say:
> nodeptr = malloc(strlen(data)+5);
> to cover the struct node* and terminating NULL, and then simply
> strcpy(nodeptr->string, data);
> The only problem I have is with compilation: I get a warning about the
> zero-length element "string". I'd like to find out the "correct" way to
> do this. I'll be glad to summarize any Emailed responses, of course.
Does this fit your needs? Change string into a char pointer, then allocate
dynamic memory for the string and save the pointer into nodeptr->string.
struct node {
struct node* next;
char *string;
} *nodeptr;
nodeptr = malloc(sizeof(struct node));
nodeptr->string = malloc(strlen(data)+1);
strcpy(nodeptr->string,data);
Good Luck!
More information about the Comp.lang.c
mailing list