What is a designated initializer in C?
Asked Answered
C

3

44

I have an assignment that requires me to understand what are designated initializers in C, 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?

Conch answered 9/11, 2017 at 13:3 Comment(1)
Comments have been moved to chat; please do not continue the discussion here. Before posting a comment below this one, please review the purposes of comments. Comments that do not request clarification or suggest improvements usually belong as an answer, on Meta Stack Overflow, or in Stack Overflow Chat. Comments continuing discussion may be removed.Burchfield
T
51

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.)
Trucker answered 9/11, 2017 at 13:5 Comment(5)
Take a look at this for further reading about C++. I believe it's well set for the next standard.Radke
@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.Trucker
port70.net/~nsz/c/c11/n1570.html#6.7.9p9 at least unnamed structure members are uninitialized.Undersize
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.Stevana
In struct Foo foo { .a = ... should there be an equal sign like this: struct Foo foo = { .a = ...Reprehension
W
28

The Designated 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 };

Designated 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; };

Designated 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 };

Designated Initialization

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

4. Others:

Labelling 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

Wont answered 3/1, 2021 at 4:39 Comment(0)
W
0

C99 standard draft

This is not going to be particularly enlightening, but just to satisfy my own morbid curiosity: WG14/N1256 C99 standard draft

6.7.8 Initialization

designator-list:
  designator
  designator-list designator

designator:
  [ constant-expression ]
  . identifier

Constraints

6 If a designator has the form

[ constant-expression ]

then the current object (defined below) shall have array type and the expression shall be an integer constant expression. If the array is of unknown size, any nonnegative value is valid.

7 If a designator has the form

. identifier

then the current object (defined below) shall have structure or union type and the identifier shall be the name of a member of that type.

Semantics

17 Each brace-enclosed initializer list has an associated current object. When no designations are present, subobjects of the current object are initialized in order according to the type of the current object: array elements in increasing subscript order, structure members in declaration order, and the first named member of a union.129) In contrast, a designation causes the following initializer to begin initialization of the subobject described by the designator. Initialization then continues forward in order, beginning with the next subobject after that described by the designator

18 Each designator list begins its description with the current object associated with the closest surrounding brace pair. Each item in the designator list (in order) specifies a particular member of its current object and changes the current object for the next designator (if any) to be that member.131) The current object that results at the end of the designator list is the subobject to be initialized by the following initializer.

EXAMPLE 9 Arrays can be initialized to correspond to the elements of an enumeration by using designators:

enum { member_one, member_two };
const char *nm[] = {
    [member_two] = "member two",
    [member_one] = "member one",
}

EXAMPLE 11 Designators can be used to provide explicit initialization when unadorned initializer lists might be misunderstood:

struct { int a[3], b; } w[] =
    { [0].a = {1}, [1].a[0] = 2 };
Westernism answered 28/2, 2023 at 8:50 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.