Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!ncar!ames!lll-winken!uunet!fozzy!ellis From: ellis@fozzy.UUCP (Randy Ellis) Newsgroups: comp.lang.c Subject: Re: Indefinite-length array as member of struct: how? Summary: a solution. Keywords: char string [] [0] [1] Message-ID: <821@fozzy.UUCP> Date: 17 Jul 89 16:54:44 GMT References: <7360@c3pe.UUCP> Organization: Rockwell International - Richardson, Texas Lines: 22 In article <7360@c3pe.UUCP>, charles@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!