Auto variable with sizeof == 0

drw at cullvax.UUCP drw at cullvax.UUCP
Thu Feb 12 08:00:06 AEST 1987


braner at batcomputer.tn.cornell.edu (braner) writes:
> In the famous "microEMACS" by David Conroy, which has been widely
> utilized and modified, the basic text-line structure looks like this:
> 
> typedef struct LINE {
> 	struct LINE *nextline;
> 	struct LINE *prevline;
> 	short       size;		/* s.b. int! */
> 	short       used;
> 	char        text[];		/* !!!!!!!!! */
> }	LINE;
> 
> The idea is to allocate memory for lines as follows:
> 
> 	lineptr = malloc(sizeof(LINE)+length);
> 
> where length is as needed at the time for that line.  The actual text
> of the line is stored OUTSIDE the struct, starting at lineptr->text[0].
> This is, of course, "illegal".  Some compilers give a warning about
> "zero-size structure element".
> 
> Question:  Do some compilers refuse to accept this?  Is there a GOOD
> way to do it legally?  (NOTE: I KNOW that you can use:

Replace "char text[]" with "char text[0]".  This leaves the
declaration perfectly legitimate.  Probably it isn't kosher according
to ANSI to reference foo.text[27], but the various requirements that
ANSI puts on make it extremely likely that it will work in any
conforming implementation.

Dale
-- 
Dale Worley		Cullinet Software
UUCP: ...!seismo!harvard!mit-eddie!cullvax!drw
ARPA: cullvax!drw at eddie.mit.edu



More information about the Comp.lang.c mailing list