1

Consider the following example which initialize an array with a default value:

static unsigned int array[10] = { [ 0 ... 9 ] = 5 };

What exactly this operator does?

It is related to variadic macro __VA_ARGS__?

bogdan tudose
  • 1,064
  • 1
  • 9
  • 21

2 Answers2

5

In standard C, since C99, designated initializers allow to initialize individual elements of an array in the form:

int array[4] = {[1] = 42};

The syntax you stumbled upon is a range initializer, it is a GNU extension to initialize all elements between 0and 9 to the given value, thus strictly equivalent to:

static unsigned int array[10] = { [0] = 5, [1] = 5, [2] = 5, [3] = 5, [4] = 5, [5] = 5, [6] = 5, [7] = 5, [8] = 5, [9] = 5};

only less a burden to type and read.

Reference

joH1
  • 555
  • 1
  • 9
  • 27
2

Nothing in ISO C. It's a nonstandard construct.

In GNU C (gcc/clang) it appears to initialize each of elements 0 through 9 to 5, i.e., it's shorthand for (C99)

static unsigned int array[10] = { [0]=5, [1]=5, [2]=5, /*...*/ [9]=5 };

or (C89)

static unsigned int array[10] = { 5, 5, 5, 5, /*...*/ };

The ... extension also works for cases:

_Bool lowercase_eh(char c)
{
    switch(c) case 'a' ... 'z': return 1;
    return 0; 
}

Apart from using the same ... token, it is unrelated to variadic macros or fucntions.

Petr Skocik
  • 58,047
  • 6
  • 95
  • 142