Zero Length Arrays Allowed in C Standard?

Fred Smith fredex at cg-atla.UUCP
Thu Dec 7 01:32:48 AEST 1989



OK. Thanks to all of you who sent me good answers to my question about
reasons for zero-length arrays.

Now I think I understand. In fact, I recall having seen code previously
which used a zero-length array at the tail end of a structure. I thought
at the time that it was a HORRIBLE way to write code. I STILL think so.
Sure, it works (on some implementation!!!!). Sure, its also convenient
and useful. Does that make it good C?  Does that make it easy to figure
out when you trip over it for the first time in somebody's undocumented
piece of code?  NO.

I think that its convenience is not sufficient reason to insist that
it should be a part of the language. After all, there are other
conveniences which are used in certain implementations that are also not
a part of the language as specified by X3J11, ususally for good reason.

One reason that comes to mind is a conflict with the way arrays 
relate to pointers. For normal arrays, simply mentioning its name
in a program evaluates to the address of the array. Now what is this
address??  It is the address of the first element of the array! If
the thing is declared as zero length, what do you get when you
mention its address ??  I dunno, haven't tried it yet (I intend to),
but I bet it makes no sense, whatever it is! Even if it does evaluate
to some address somewhere, it certainly is not an address at which
one would dare to store data (without, of course, the use of malloc
to create some data space there)! I would guess that it is because of this kind
of sticky issue that the committee chose not to embrace zero-length
arrays.

Sure, it would be nice if everybody's favorite feature (or abuse of the
language) were specified in the standard. However, the guys onthe committee
were also trying to make all the pieces fit together in a more-or-less
logical manner, so as to reduce some of the hidden gotchas, and it seems
to me that this particular one does have a few hidden gotchas. It certainly
is easy enough to work around the "lack" of this "feature".

Fred



More information about the Comp.lang.c mailing list