What does this mean in c? square brackets inside braces

const int status[STATUS_SIZE] = {
        [0] = -1, 
        [1] = 0,
        [2] = 1,
};

and

const char *messages[MESSAGE_SIZE] = {
        [0] = "OK",
        [1] = "NG",
};

Can you explain?

+4
source share
1 answer

C99 introduces the assigned initializers , with which you can initialize the array in any order using the index.

6.27 Assigned Initializers

The C90 standard requires that initializer elements be displayed in a fixed order, just like the order of elements in an initialized array or structure.

In ISO C99, you can specify elements in any order by specifying the array indexes or the names of the structure fields to which they apply, and GNU C allows this as an extension in C90 mode. This extension is not implemented in GNU C ++.

, [index] = . ,

int a[6] = { [4] = 29, [2] = 15 };

int a[6] = { 0, 0, 15, 0, 29, 0 };
+4

Source: https://habr.com/ru/post/1692395/


All Articles