Indefinite-length array as member of struct: how?

Markku Savela savela at tel2.tel.vtt.fi
Sun Jul 9 07:29:08 AEST 1989


In article <8870 at venera.isi.edu>, lmiller at venera.isi.edu (Larry Miller) writes:
> In article <7360 at c3pe.UUCP> charles at c3pe.UUCP (Charles Green) writes:
>>struct node {
>>	struct node* next;
>>	char	string[];
>>} *nodeptr;
>
> 		nodeptr = malloc(strlen(data)+1 + sizeof(struct node *));

   This the prime example where the ANSI "offsetof"-macro becomes handy.
I write the above

	(struct node *)malloc(offsetof(struct node,string)+strlen(data)+1);

   About the original question, I guess we have to resign to using
either "string[1]" or "string[HUGE_NUMER]" to satisfy picky compilers.
(Isn't this one of the recurring questions of comp.lang.c?)
--
  Markku Savela



More information about the Comp.lang.c mailing list