Take the 2-minute tour ×

I came across this code construct in Linux and would like to understand it

struct mystruct {
  int x;
  int b[40];

/*later */

static struct mystruct e = { .x = 5,
                             .b = {-1},   

What does .b = {-1} do ? Does it initialize only the first or all elements of array b ? How does it work ?

share|improve this question
Designated Initializers: gcc.gnu.org/onlinedocs/gcc/Designated-Inits.html –  Blue Moon May 9 '12 at 12:56

2 Answers 2

up vote 3 down vote accepted
static struct mystruct e = {
    .x = 5,
    .b = {-1},   

here it initializes b[0] to -1. other elements are initialized to 0.

share|improve this answer

It means initialize the member b of the struct with an array that starts with -1 and followed by 0

  • The .b = is a GCC extensions. (as commented, from C99 this is part of the standard as well)
  • The {-1} is standard array initialization.
share|improve this answer
Designated initialisers were introduced in C99 (but IIRC gcc is the only platform that implements c99) So, calling it a gcc extension would be unfair. –  wildplasser May 9 '12 at 12:38
Considering the positioning of the code (inside the braces of a struct initialization), I don't think it's totally correct to call the = part of the standard notation. Before C99, you would have written this as e = { 5, { -1 } };, without the equals. –  unwind May 9 '12 at 12:42
@wildplasser - thanks for the comment, I usually think ANSI C... –  MByD May 9 '12 at 12:43
@unwind - fair enough :) will edit as well. –  MByD May 9 '12 at 12:44

Your Answer


