empty array declarations

throopw at dg_rtp.UUCP throopw at dg_rtp.UUCP
Sat Oct 18 06:56:11 AEST 1986


> judah at whuxcc.UUCP (Judah Greenblatt)

> Can anyone tell me why the following program is NOT in error?
No, because it IS in error.

> Can anyone tell me what it MEANS?
No, because it is meaningless.

(Please take both of the above answers to be flip or humorous.)

More seriously, declaring an automatic array with the number of elements
missing isn't stated explicitly to be an error in K&R, but we can infer
that it is meaningless, since there is no way to deduce the array size
by rules that *are* in K&R in such a case.  And both H&S and the draft
ANSI C standard explicitly make it illegal.

SysV.2 lint and many pcc compilers fail to note this error, and seem to
treat it as an array with no elements at all (and allocate no storage
for it, only an address... any subscription of the array at all is
out-of-bounds... not too useful).  Some compilers that label a
definition of an array with explicitly zero elements (as in "int a[0];")
as an error nevertheless treat "int a[];" as an array with implicitly
zero elements.  "Hard to believe!"

The DG C compiler says:

        int a[];
            ^
    You did not specify the array bounds for "a",
    either directly or implicity within an initialization.  You can omit
    only the first bound of an extern declaration or the first bound after
    a pointer.  The compiler used a bound of 1.

which is a tad friendlier than what pcc compilers do with it, which is
usually cause your program to blow up mysteriously later.

--
There are two ways to write error-free programs; only the third one works.
    --- Alan J. Perlis
-- 
Wayne Throop      <the-known-world>!mcnc!rti-sel!dg_rtp!throopw



More information about the Comp.lang.c mailing list