20

I know this might be a basic question.

I have an assignment that requires me to understand what a Designated initializers in C are and what it means to initialize a variable with one.

I am not familiar with the term and couldn't find any conclusive definitions.

What is a designated initializer in C?

Federico Baù
  • 3,772
  • 4
  • 18
  • 27
user7349461
  • 227
  • 1
  • 2
  • 5
  • look here https://gcc.gnu.org/onlinedocs/gcc-4.9.2/gcc/Designated-Inits.html – CIsForCookies Nov 09 '17 at 13:05
  • 4
    I'm voting to close this question as off-topic because no serious research done. – chux - Reinstate Monica Nov 09 '17 at 13:09
  • 2
    @chux negative though, I was looking for a duplicate for that but couldn't find one, only some that show how to initialize *either* an array *or* a struct, but not even both. – Antti Haapala -- Слава Україні Nov 09 '17 at 13:19
  • 4
    @chux SO is supposed to be a repository of high-quality answers to relevant questions, right? As it happened, *I* had this question today, and was happy to find Bathsheba's concise answer, in a place I can trust. (Yes, I should have my own copy of the C11 standard, but alas, I don't. And anyway, the Standards aren't always the best places for mere mortals to find answers, so a Q and an A here seem perfectly appropriate.) – Steve Summit Jul 03 '18 at 17:37
  • Just watched Jason explaining about it here on C++ Weekly channel https://www.youtube.com/watch?v=44rs_hX1dxE – Tien Do Dec 19 '18 at 01:02

2 Answers2

28

Designated initialisers come in two flavours:

1) It provides a quick way of initialising specific elements in an array:

int foo[10] = { [3] = 1, [5] = 2 };

will set all elements to foo to 0, other than index 3 which will be set to 1 and index 5 which will be set to 2.

2) It provides a way of explicitly initialising struct members. For example, for

struct Foo { int a, b; };

you can write

struct Foo foo { .a = 1, .b = 2 };

Note that in this case, members that are not explicitly initialised are initialised as if the instance had static duration.


Both are standard C, but note that C++ does not support either (as constructors can do the job in that language.)
Bathsheba
  • 227,678
  • 33
  • 352
  • 470
  • Take a look at [this](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/p0329r4.pdf) for further reading about C++. I believe it's well set for the next standard. – DeiDei Nov 09 '17 at 13:10
  • @AnttiHaapala: Need to dig out my standard for my final comment before the ruled line. (Alas I'm on the choo choo). I *think* it's correct. – Bathsheba Nov 09 '17 at 13:27
  • http://port70.net/~nsz/c/c11/n1570.html#6.7.9p9 at least unnamed structure members are uninitialized. – Antti Haapala -- Слава Україні Nov 09 '17 at 13:29
  • The members (of the variable) that are not explicitly initialized in the code will be automatically initialized by the compiler to 0. Much like all static and global variables are always automatically initialized to 0 when not done explicitly in the code. – abetancort Jun 26 '20 at 12:49
8

The Designed Initializer came up since the ISO C99 and is a different and more dynamic way to initialize in C when initializing struct, union or an array.

The biggest difference to standard initialization is that you don't have to declare the elements in a fixed order and you can also omit element.

From The GNU Guide:

Standard C90 requires the elements of an initializer to appear in a fixed order, the same as the order of the elements in the array or structure being initialized.

In ISO C99 you can give the elements in random order, specifying the array indices or structure field names they apply to, and GNU C allows this as an extension in C90 mode as well


Examples

1. Array Index

Standard Initialization

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

Designed Initialization

  int a[6] = {[4] = 29, [2] = 15 }; // or
  int a[6] = {[4]29 , [2]15 }; // or
  int widths[] = { [0 ... 9] = 1, [10 ... 99] = 2, [100] = 3 };

2. Struct or union:

Standard Initialization

struct point { int x, y; };

Designed Initialization

 struct point p = { .y = 2, .x = 3 }; or
 struct point p = { y: 2, x: 3 };

3. Combine naming elements with ordinary C initialization of successive elements:

Standard Initialization

int a[6] = { 0, v1, v2, 0, v4, 0 };

Designed Initialization

int a[6] = { [1] = v1, v2, [4] = v4 };

4. Others:

Labeling the elements of an array initializer

int whitespace[256] = { [' '] = 1, ['\t'] = 1, ['\h'] = 1,
                        ['\f'] = 1, ['\n'] = 1, ['\r'] = 1 };

write a series of ‘.fieldname’ and ‘[index]’ designators before an ‘=’ to specify a nested subobject to initialize

struct point ptarray[10] = { [2].y = yv2, [2].x = xv2, [0].x = xv0 };

Guides

Federico Baù
  • 3,772
  • 4
  • 18
  • 27