Zero Length Arrays Allowed in C Standard?

Doug Gwyn gwyn at smoke.BRL.MIL
Tue Dec 5 16:56:55 AEST 1989


In article <8129 at cg-atla.UUCP> fredex at cg-atla.UUCP (Fred Smith) writes:
>Why the !@#$ would anyone even want to declare an array of zero size ????

There are several contexts in which it would be useful.

The general philosophical answer is, since it could be useful
and since we all agree that control structures that can loop
zero times are preferable to ones that insist on looping at
least once, why shouldn't the same considerations be applied
to the data structures manipulated by such control structures?

For example, suppose that one can individually configure a
bunch of options when compiling an application, and that OPTA,
etc. are either 1 or 0 to determine whether or not the option
is to be supported at run time.  Then one might find it
convenient to have an array of run-time option enable/disable
flags, or some similar data structure:
	bool	opt_on[OPTA+OPTB+OPTC+...+OPTZ];
In the case where no option cupport was configured at compile
time, it would be nice if the resulting 0-length array could
merely be left as is, since we know that no accesses will be
made to its contents at run time.  Yet according to the C
standard, that would be an illegal declaration, so we have to
code in a special kludge just to take care of this special
case.



More information about the Comp.lang.c mailing list