Path: utzoo!utgpu!water!watmath!clyde!att!osu-cis!tut.cis.ohio-state.edu!mailrus!cornell!uw-beaver!uw-june!pardo From: pardo@june.cs.washington.edu (David Keppel) Newsgroups: comp.lang.c Subject: Re: Variable-length messages. Message-ID: <6175@june.cs.washington.edu> Date: 22 Oct 88 02:26:52 GMT References: <21602@watmath.waterloo.edu> Reply-To: pardo@cs.washington.edu (David Keppel) Organization: U of Washington, Computer Science, Seattle Lines: 35 >[ want var-size structures: struct foo { int siz; char c[1] }; ] To make the code a little clearer, have a global #define: /* * When this is used, it means that the structure it appears * in can be allocated by malloc() as an arbitrary-size * structure. */ #define VARSIZE 1 then you can do: struct foo { unsigned size; your_type storage[VARSIZE]; } Mallocing them is still a trifle wierd: thing = malloc (sizeof(unsigned) + n * sizeof(your_type)); because the structure could have holes in it. (Yecch.) Slightly more portable: thing = malloc (sizeof(struct foo) - sizeof(unsigned) + (n-1)*sizeof(your_type)); I'm not sure if this is portable. I appeal to the net gods for further light. Anybody? ;-D on ( My ignorance knows no bounds-checking ) Pardo -- pardo@cs.washington.edu {rutgers,cornell,ucsd,ubc-cs,tektronix}!uw-beaver!june!pardo