The following code gives an “invalid initializer” error:
int a[]=(1,2,3);
But the following compiles successfully although it considers ‘,’ as comma OPERATOR and not SEPARATOR:
int a[][2]={(1,2),(3,4)};
So why is () invalid for a 1D array and not for a 2D array?
In the first example:
the initializer is a (rather odd) expression of type
int. (It contains two comma operators, and yields the value3.) The object is an array. The initialization is invalid because it’s a type mismatch.The second:
is equivalent to:
which is valid because it’s permissible to omit nested curly braces in an initializer; elements are used to initialize successive elements of the object. The first and third commas are comma operators; the second is a delimiter.
The outermost curly braces are optional if the initializer is simply an expression of the target type, whether it’s a scalar, struct, or union. For example, you can write:
The outermost curly braces are required for an initializer that specifies element values (for an array, struct, or union object).
For example:
is valid — but it’s more clearly written as:
Apart from the first example being invalid, both are poor style. The first may have been intended to be:
and the second either:
or
depending on the intent.