Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!watmath!clyde!rutgers!mit-eddie!genrad!decvax!necntc!cullvax!drw From: drw@cullvax.UUCP Newsgroups: comp.lang.c Subject: Auto variable with sizeof == 0 Message-ID: <795@cullvax.UUCP> Date: Wed, 11-Feb-87 17:00:06 EST Article-I.D.: cullvax.795 Posted: Wed Feb 11 17:00:06 1987 Date-Received: Fri, 13-Feb-87 05:37:08 EST Organization: Cullinet Software, Inc., Westwood, MA Lines: 35 braner@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@eddie.mit.edu