Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!utgpu!water!watmath!clyde!rutgers!iuvax!pur-ee!j.cc.purdue.edu!h.cc.purdue.edu!s.cc.purdue.edu!mit-prep!jym From: jym@mit-prep.UUCP Newsgroups: comp.lang.c Subject: Arrays of Unknown Length in Structures (and sizeof) Message-ID: <243@mit-prep.ARPA> Date: Tue, 22-Sep-87 14:13:39 EDT Article-I.D.: mit-prep.243 Posted: Tue Sep 22 14:13:39 1987 Date-Received: Sat, 26-Sep-87 15:28:49 EDT Organization: The MIT AI Lab, Cambridge, MA Lines: 47 Consider the following: struct STRUCT_FOO { int one; int two; int open[]; }; main() { extern void *malloc(); short int i; struct STRUCT_FOO *foo_p; printf("Size = %d\n",sizeof (struct STRUCT_FOO)); foo_p = malloc(sizeof (struct STRUCT_FOO) + (7 * sizeof (int))); for (i = 0; i < 7; ++i) foo_p->open[i] = i * 100; for (i = 0; i < 7; ++i) printf("open[%d] = %d\n",i,foo_p->open[i]); } I've tried this on two systems: VMS V4.5 running VAX C V2.3, and BSD 4.2 running "cc". Both do what I want them to do, though "cc" gives a warning message for line 6. My question: is this kosher? I've seen people do this by declaring the struct with an array of 1 element ("int open[1];"), but this seems a bit kludgy to me. Declaring it as an array of 0 elements ("int open[0];") works with "cc" (albeit with two warning messages) but VAX C won't compile it at all. As far as I'm concerned, if this isn't kosher, it should be! <_Jym_> -- jym@prep.ai.mit.edu -- A STRANGE GAME. -- .signature file under construction -- THE ONLY WINNING MOVE IS -- read at your own risk -- NOT TO PLAY. --