Questions tagged [designated-initializer]

In Cocoa programming, a designated initializer is the method through which all of the instance's initial parameters can be set. A designated initializer is typically executed implicitly when not called explicitly. In C programming, designated initializers refer to a certain way of struct/union/array initialization, which allows the programmer to to initialize an element by using its name or index.

In Cocoa programming, a designated initializer is a method that accepts all of the instance's initial parameters and initializes the instance. This method is typically something other than - (id)init, and is usually documented as the designated initializer in the header file. A designated initializer is typically executed implicitly when not called explicitly. As an example, the designated initializer of UIViewController is - (id)initWithNibName:bundle:.


In C programming, designated initializers refer to a certain way of struct/union/array initialization, which allows the programmer to to initialize an element by using its name or index. This feature was introduced with the C99 standard.

Designated initializers have two forms (ISO 9899:2011 6.7.9):

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.

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.

Example of designated initializer usage (9899:2011 6.7.9/35):

struct { int a[3], b; } w[] =
       { [0].a = {1}, [1].a[0] = 2 };

In the example, both the above mentioned forms of designated initializers are combined.

135 questions
14
votes
1 answer

C++20 designated initializers with templated types

How are designated initializers (C++20) supposed to work with CTAD? This code works fine in gcc9.2, but fails with clang8 template struct my_pair { int_t first; float_t…
14
votes
3 answers

C99 Structure Designated Initialisers and other value

I am aware that in C99 you can initialize members of the structure using member name as follows : struct myStruct { int i; char c; float f; }; So following is valid : struct myStruct m = {.f = 10.11, .i = 5, .c = 'a'}; Also it is said that…
Sagrian
  • 1,048
  • 2
  • 11
  • 29
12
votes
2 answers

Constructor interferes with member variable designated initializer?

For a while now, one has been able to use "designated initializer" in GCC: struct CC{ double a_; double b_; }; CC cc{.a_ = 1., .b_ = 2.}; assert(cc.a_ == 1. and cc.b_ == 2.); // ok CC cc{.bla = 0., .bli = 0.}; // compile error However when…
alfC
  • 14,261
  • 4
  • 67
  • 118
12
votes
2 answers

What causes this initializer inheritance issue with UIBarButtonItem?

I'm trying to create a simple Swift subclass of UIBarButtonItem: class LabelBarButtonItem: UIBarButtonItem { let label: UILabel init(text: String) { self.label = UILabel(frame: CGRectZero) self.label.tintColor = UIColor.grayColor() …
Bill
  • 44,502
  • 24
  • 122
  • 213
11
votes
2 answers

Why I can not use designated initalizers with structs that are not aggregates?

C++ has a nice new feature: struct Point{ int x; int y; int z; }; Point p{.x=47, .y=1701, .z=0}; But if I add a constructor then I am forbidden from using the nice designated initalizers syntax: struct Point{ Point(int x, int y, int z = 0): x(x),…
NoSenseEtAl
  • 28,205
  • 28
  • 128
  • 277
11
votes
1 answer

Possible compiler bug in MSVC12 (VS2013) with designated initializer

Using VS2013 Update 2, I've stumbled on some strange error message : // test.c int main(void) { struct foo { int i; float f; }; struct bar { unsigned u; struct foo foo; double d; }; …
diapir
  • 2,872
  • 1
  • 19
  • 26
10
votes
1 answer

CTAD and designated initializers in C++20

I have already stated confusion about CTAD with designated initializers in this question, but i have another confusion with a very similar code snippet template struct my_pair { int_t first; …
nnolte
  • 1,628
  • 11
  • 25
10
votes
2 answers

Are the elements present after a designator initializes elements in strictly increasing manner?

Here, I have initialized array like this : #include int main() { int a[10] = {1, 2, 3, [7] = 4, 8, 9}; printf("a[7] = %d\na[8] = %d\na[9] = %d\n", a[7], a[8], a[9]); return 0; } Output : a[7] = 4 a[8] = 8 a[9] = 9 Here,…
user7620837
10
votes
2 answers

Cryptic struct definition in C

I came across the following maze definition code: typedef struct mazeNode { int hasCheese; int tag; struct mazeNode *left; struct mazeNode *right; } maze_t; maze_t maze = { .tag = 1, .left = &(maze_t) { .left =…
liv2hak
  • 14,472
  • 53
  • 157
  • 270
9
votes
3 answers

Combine designated initializers and malloc in C99+?

Is there a nice way to combine designated initializers from C99, with the result of a malloc? The following seems to have needless duplication: typedef struct { int a, b, c; } Type; Type *t = malloc(sizeof *t); *t = (Type) { .a = 2, .b =…
Matt Joiner
  • 112,946
  • 110
  • 377
  • 526
9
votes
4 answers

Override designated initializer of superclass

I am reading a book which has a guideline: "If a class declares a designated initializer that is different from its superclass, the superclass’s designated initializer must be overridden to call the new designated initializer" As I understand this…
user2568508
9
votes
2 answers

Why can't a designated initializer call a secondary initializer in its base class?

According to the documentation, a class's designated initializer in Objective-C must call the designated initializer of its base class. Another rule is that secondary initializers must call the designated initializer of their own class. But if the…
Halt
  • 2,924
  • 4
  • 22
  • 26
7
votes
2 answers

Does designated initializer of sub-aggregate require curly braces?

In the following program, aggregate struct B has the field a, which is itself an aggregate. Can C++20 designated initializer be used to set its value without surrounding curly braces? struct A { int i; }; struct B { A a; }; int main() { …
Fedor
  • 17,146
  • 13
  • 40
  • 131
7
votes
1 answer

Why does this code using designated initializers in function parameters goes from ambiguous to not compiling when removing one function?

Consider the following code: struct A{ int x; int y; }; struct B{ int y; int x; }; void func (A){ } void func (B){ } int main() { func({.y=1,.x=1}); } For some reason both clang and gcc consider this code ambiguous even though…
NoSenseEtAl
  • 28,205
  • 28
  • 128
  • 277
6
votes
5 answers

Is it possible to get pointer to the 'this' structure, when using designated initializer?

This kind of struct is used as head of linked list: struct lista { struct lista* next; struct lista* prev; }; When next and prev both points to struct itself, then the list is empty. The following macro can be used for initializing the…
SKi
  • 8,007
  • 2
  • 26
  • 57
1
2
3
8 9