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?
In Swift, an initializer is a special function that we use to create objects of a particular class, struct, or other type. Initializers are sometimes called constructors, because they “construct” objects. Let's take a look at an example.
With C++20, we get a handy way of initializing data members. The new feature is called designated initializers and might be familiar to C programmers.
Designated initializers are the primary initializers for a class. A designated initializer fully initializes all properties introduced by that class and calls an appropriate superclass initializer to continue the initialization process up the superclass chain.
Types of Swift InitializersDesignated Initializers: This is the primary initializer of the class. It must fully initialize all properties introduced by its class before calling any superclass initializer. A class can have more than one designated initializer. Every class must have at least one designated initializer.
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.
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
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 };
Standard Initialization
struct point { int x, y; };
Designed Initialization
struct point p = { .y = 2, .x = 3 }; or
struct point p = { y: 2, x: 3 };
Standard Initialization
int a[6] = { 0, v1, v2, 0, v4, 0 };
Designed Initialization
int a[6] = { [1] = v1, v2, [4] = v4 };
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 };
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With