6
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 an array of a structs that I found in the Linux kernel source. I have never seen this form of initialization before and I couldn't simulate the same thing on my own. What am I missing actually?

Source of the code

torrential coding
  • 1,755
  • 2
  • 24
  • 34
Kooi Nam Ng
  • 309
  • 3
  • 11

2 Answers2

6

It is a GNU/GCC extension called Designated Initializers. You can find more information about it in the GCC documentation.

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

ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
  • 3
    Just for accuracy, designated initializers aren't a GCC extension ([see this question](http://stackoverflow.com/questions/9849719/what-do-square-brackets-mean-in-array-initialization-in-c)), but the range syntax is. – huon Mar 29 '12 at 07:05
5

It is done by using a Designated Initializer.

It is a gcc extension and not standard c construct. Using it results in non portable code, So avoid using such compiler extensions unless portability is least of your concerns.

Alok Save
  • 202,538
  • 53
  • 430
  • 533