[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Bug#177303: Accepts illegal declaration "int x[2, 3];" in C99 mode

Anthony DeRobertis <asd@suespammers.org> writes:

> > The declaration
> >
> > int x[2, 3];
> >
> > is not legal in C99, since "2, 3" is not an assignment-expression.
> I'm not a C99 language lawyer, but are you sure? Is the comma operator
> allowed there, or would one have to write "int x[(2,3)]" to get that
> behavior?

The definition of a direct-declarator, in 6.7.5/1, is

    ( declarator )
    direct-declarator [ type-qualifier-list-opt assignment-expr-opt ]
    direct-declarator [ static type-qualifier-list-opt assignment-expr ]
    direct-declarator [ type-qualifier-list static assignment-expr ]
    direct-declarator [ type-qualifier-list-opt * ]
    direct-declarator ( parameter-type-list )
    direct-declarator ( identifier-list-opt )

So you must have an assignment-expr in the brackets. The comma
operator is defined in 6.5.17

                       expression , assignment-expr

so it is only allowed in expression, not in assignment-expression.

Writing "int x[(2,3)];" is most likely ill-formed as well (depending
on context). 2,3 is not a constant expression, as 6.6/3 says

       [#3]  Constant  expressions  shall  not  contain assignment,
       increment, decrement,  function-call,  or  comma  operators,
       except  when  they are contained within a subexpression that
       is not evaluated.

So in C89, "int x[(2,3)];" is ill-formed, as the array bounds must be
a constant expression. In C99, this declares a variable-length array,
which may or may not be allowed depending on context.


Reply to: