Ignoring static
and *
(for an omitted size) in between the []
brackets, the syntax for an array declarator is (from C99 TC3 (n1256) 6.7.5 p1; C11 (n1570) 6.7.6 p1):
direct-declarator: direct-declarator [ type-qualifier-listopt assignment-expressionopt ] [...]
Thus, a declaration like
int foo[0,1];
is a syntax error, but
int foo[(0,1)];
is allowed (at block scope, as this is a VLA).
There are certain cases where arbitrary expressions aren't allowed because this would cause ambiguity with comma used as a separator. For example, the arguments of a function call must be assignment expressions. I don't see, however, how such an ambiguity could be caused by
direct-declarator:
direct-declarator [ type-qualifier-listopt expressionopt ]
Would this define a strict superset of the C language? Are there examples where this grammar is ambiguous?
C89*) required a constant expression in the syntax (which is a conditional expression) so this needed to be changed for C99 to allow VLAs. But I fail to see why it was changed to an assignment expression rather than to an expression. Is there any technical reason?
Gcc (and perhaps other compilers) had VLAs as an extension before they were added to the C standard, a conflict with some other extension could also be an explanation, but I'm not aware of such an extension. Gcc 3.0.4 accepts int a[0,1];
(with -std=gnu89
and -traditional
), newer versions (tested with Gcc (Debian) 4.7.2-5) don't, so this looks unlikely to be the cause.
As far as I can see, this question equivalently applies to direct abstract declarators in type names.
*) According to this C89 draft, 3.5.4.