Designated initializer incompatibility

Hi,

The following code outputs “10 11 0” with GCC, but “10 11 7” with Clang. Is there anyone who knows which is correct? The C99 spec seems to be a bit unclear about this.

#include <stdio.h>

int main() {
int x[1][3] = {[0][2] = 7, [0] = {10, 11}};
printf("%d %d %d\n", x[0][0], x[0][1], x[0][2]);
}

Hi,

The following code outputs "10 11 0" with GCC, but "10 11 7" with Clang.
Is there anyone who knows which is correct? The C99 spec seems to be a bit
unclear about this.

GCC is correct; see
http://www.open-std.org/jtc1/sc22/wg14/www/docs/dr_253.htm

C99 6.7.8/21 says: "If there are fewer initializers in a brace-enclosed
list than there are elements or members of an aggregate, [...] the
remainder of the aggregate shall be initialized implicitly the same as
objects that have static storage duration."

So the example is equivalent to

int x[1][3] = {[0][2] = 7, [0] = {10, 11, 0}};

and the initializer for x[0][2] from the second init list overrides the
first initializer by 6.7.8/19.

#include <stdio.h>