Array initialization with a ternary operator?

c, ternary-operator

Solution

This is not valid C11.

You can only initialize an array with an initializer-list not with an expression.

int why[2] = { ... };  // initializer-list {}

Moreover, `1 == 1 ? {1,2} : {3,4}` is not a valid C expression because `{1, 2}` is not a C expression.

Just for information using compound literals you can have something close to what you want using a pointer object:

int *why = (1 == 1) ? (int[2]) {1,2} : (int[2]) {3,4};

Problem

I don't have access to the C11 specification, therefore I can't investigate this bug. The following declaration rises an error during compilation: ``` int why[2] = 1 == 1 ? {1,2} : {3,4}; ``` The error is: expected expression before { and: expected expression before :

Original source

Related problems