0

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.

mafso
  • 5,433
  • 2
  • 19
  • 40

1 Answers1

2

There is certainly no ambiguity which would result from allowing the use of the comma operator in array bounds. No doubt the committee had a reason for not allowing it, but for those of us who are not present at the discussions, it can only be speculation.

I speculate that one of the motivations was to avoid creating confusion. Many other languages allow multidimensional arrays to be declared with a comma-separated list of bounds, and someone accustomed to one of those languages might easily erroneously write int a[2,7]; intending to get a two-dimensional array. If the comma operator were allowed, this would result in the declaration of a seven-element single-dimensional array, and no error message would be produced. No error message would be produce on an attempt to index such an array either, so the mistake would be unnoted by the compiler, which might be considered unfortunate.

But that's just a wild guess.

rici
  • 234,347
  • 28
  • 237
  • 341
  • Thanks for assuring me there aren't technical reasons. The idea, that this is to avoid confusion with multi-dimensional arrays is at least a plausible guess. Sometimes someone happens to know the relevant parts of the committee discussions (some part of them is published), in which case such a question can be answered without speculation, but one cannot know when asking such a question. I leave the question open for some time, maybe someone comes across who knows... – mafso Mar 01 '15 at 11:19