I never saw before C a method of initializing an array of structures found in the original Linux kernel

55 typedef struct pidmap { 56 atomic_t nr_free; 57 void *page; 58 } pidmap_t; 59 60 static pidmap_t pidmap_array[PIDMAP_ENTRIES] = 61 { [ 0 ... PIDMAP_ENTRIES-1 ] = { ATOMIC_INIT(BITS_PER_PAGE), NULL } }; 

The code snippet above shows the initialization of the array of structures that I found in the Linux kernel source. I had never seen this form of initialization before, and I could not imitate the same thing. What am I missing?

Source code

+6
source share
2 answers

This is a GNU / GCC extension called designated initializers. More information on this can be found in the GCC documentation.

To initialize a range of elements for the same value, write [first ... last] = value . This is a GNU extension

+6
source

This is done using a designated initializer .

This is a gcc extension, not a standard c-construct. Using this result leads to non-portable code. Therefore, avoid using such extensions for the compiler, unless portability is the least of your problems.

+5
source

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


All Articles