empty array declarations

Judah Greenblatt judah at whuxcc.UUCP
Sat Oct 11 07:13:34 AEST 1986


Can anyone tell me why the following program is NOT in error?
Can anyone tell me what it MEANS?

	main()
	{
		int a[];

		a[0] = 5;
	}

Lint only complains about the random return value from main.
Most c compilers I tried (3B20, CCI 6/32, Pyramid 98x) accepted and executed
this program with no complaints.  The Sequent compiler accepted the
declaration and then gave a 'compiler error' message for the assignment.

In K&R [1], section 8.4, it states:
"..... the constant expressions which specify the bounds of the arrays may
be missing ....  This elision is useful when the array is external and the
actual definition, which allocates storage, is given elsewhere.  The first
constant-expression may also be omitted when the declarator is followed by
initialization."

This wording seems to allow the array size to be omitted anyplace,
but only assigns a meaning to the result for 'extern' arrays and where
there is an initializer.

I was recently bitten by code like this, and was wondering why it isn't
illegal to do such things.

----------
[1] Kernighan, Brian W. and Dennis M. Ritchie, "The C Programming Language",
    Prentice-Hall, Inc., 1978

-- 
Judah Greenblatt		"Backward into the Future!"
Bell Communications Research	uucp: {bellcore,infopro,ihnp4}!whuxcc!judah
Morristown, New Jersey, USA	arpa: whuxcc!judah at mouton.bellcore.com



More information about the Comp.lang.c mailing list