Is it possible to initialize only some members of an array in C already in its definition?

7

I know you can do this:

int a[5] = { 0, 3, 8, 0, 5 };

or

int a[5];
a[1] = 3;
a[2] = 8;
a[4] = 5;

Can you do the same in just one line, already in the definition?

    
asked by anonymous 22.03.2017 / 12:33

1 answer

9

It has a syntax for this:

int a[5] = { [1] = 3, [2] = 8, [4] = 5 };

It can even be in any order. This syntax is the same as doing the assignment after the declaration.

    
22.03.2017 / 12:33