Zero Length Arrays Allowed in C Standard?

William Davidsen davidsen at sungod.crd.ge.com
Tue Dec 5 06:50:42 AEST 1989


In article <8129 at cg-atla.UUCP> fredex at cg-atla.UUCP (Fred Smith) writes:

| Excuse me, but I must ask a stupid question. Why the !@#$ would anyone even want
| to declare an array of zero size ???? Isn't that rather similar to a pointer
| to the same type of object??  If so, what is wrong with declaring a pointer
| rather than an empty array ??

  A pointer is not the same as an array (of any size). One use of an
array of zero size (or one) is at the end of a struct definition. Then,
if the actual struct is allocated by malloc et al, the size allocated
can be larger than the size of the struct as defined, and the array will
be addressable using positive non-zero subscripts.

  The array must be used instead of the pointer to insure that
allignment considerations don't creep in. The portable solution is to
declare the array with size one, and then adjust the size of the malloc
call as needed.

	bill davidsen		(davidsen at crdos1.crd.GE.COM)
  {uunet | philabs}!crdgw1!crdos1!davidsen
"Stupidity, like virtue, is its own reward" -me



More information about the Comp.lang.c mailing list