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
3
votes
1 answer

clang can remove function call when order of designated initializers does not correspond to fields declaration

this code: #include struct Acc { int a; }; struct Buu { int b; }; struct Foo { const Acc& acc; Buu& buu; }; void printInfo( const Foo& ) { std::cout << "hi!" << std::endl; } void call( Buu& buu ) { Acc acc = { 1…
3
votes
2 answers

Combine two designated initializers via a macro

In an embedded project, I use a library that provides a macro for initializing a structure. This provides reasonable defaults, but the defaults depend on other parameters. I want to override one or more values of this designated initializer, because…
BasilFX
  • 43
  • 2
  • 8
3
votes
1 answer

Initialize subclass of NSTextView without providing NSTextContainer

I have a subclass of NSTextView, and in my initializer of this subclass I would like to call: super.init(frame: NSMakeRect(...)) which is what I always do when initializing an NSTextView programmatically. I simply write: let textView =…
3
votes
2 answers

Is there a Way to Get Warned about Misbehaving Designated Initializers?

C99 introduced the concept of designated intializers for structs. So for example, given: typedef struct { int c; char a; float b; } X; I could initialize like: X foo = {.a = '\1', .b = 2.0F, .c = 4}; and calling: printf("c = %d\na =…
Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
3
votes
1 answer

Must you override init when you create a new designated initializer?

I have read up quite a bit on designated and convenience initializers in objective-c and feel I have a good understanding of how and why they are used. I just have a couple of outstanding questions, well one really. Say you have a class that…
cheznead
  • 2,589
  • 7
  • 29
  • 50
3
votes
1 answer

Non-designated initialiser inheritance from Objective-C classes

Having come across problems when sub-classing UIKit classes and adding immutable variables to them, I made a test project to figure out what was going on. My conclusion is that if: we have an Objective-C class, which inherits from another class,…
Rupert
  • 2,097
  • 19
  • 28
3
votes
0 answers

Initializing struct within another struct using designated initializer causes compile error in Visual Studio 2013

In Visual Studio 2013, the following code snippet generates the compile error error C2440: 'initializing' : cannot convert from 'TestSubStruct' to 'int' #include typedef struct TestSubStruct { int test; } TestSubStruct; typedef…
3
votes
1 answer

Zeroing an Unusual Array

I have a bit of C code, reproduced below. It is my understanding that it is setting certain bits of the BootPML4 array to a certain value. Could someone please explain how the BootPML4 array below is filled? Also, how do I ensure that unused values…
wjk
  • 1,219
  • 11
  • 27
3
votes
2 answers

Must an Objective-C class have exactly one designated initializer?

I found some info of the designated initializer in this Apple's docs, but what I don't understand is, must each class have one and only one designated initializer? For example, what if class A has initL, initM, initN, while class B inherits from…
nonopolarity
  • 146,324
  • 131
  • 460
  • 740
2
votes
1 answer

Is referencing a member during Initialization valid?

I have a struct that contains multiple members. these members should be constructed using another member. Is accessing this other member for the initialization of the members valid, or am I invoking UB this way? struct Data { int b; }; struct…
Raildex
  • 3,406
  • 1
  • 18
  • 42
2
votes
0 answers

Non-Designated Initializers (Compiler Flag Warning)

Let us assume we have the following code: struct MyStruct { double height, width; }; int main() { const MyStruct a { 5.1, 2.3 }; const MyStruct b { .height = 3.5, .width = 1.8 }; return 0; } Since C++20 we have Designated…
Phil-ZXX
  • 2,359
  • 2
  • 28
  • 40
2
votes
0 answers

Pretty-printing Designated Initializers in Visual Studio 2022

C++ supports designated initializers officially since C++20, C supports them since C99. How can Visual Studio C++ 2022 be configured to pretty-print them with a space after the opening curly bracket? I think Visual Studio internally uses…
2
votes
2 answers

Using variable being initialized in a designated initializer

Am I allowed to use a variable being initialized inside a designated initializer? Consider the following listing: struct A { int a; int * const a_ptr; }; struct A foo(int a) { struct A result = { .a = a, .a_ptr =…
ivaigult
  • 6,198
  • 5
  • 38
  • 66
2
votes
4 answers

C99 Initialize an array through a pointer with braces

I wrote a function that calculates all for vertex points for a square given its position with and height. Since one cannot return array's in C I have to do it through a pointer. This is the code I ended up writing: // Creates a rectangle for mapping…
Nils
  • 13,319
  • 19
  • 86
  • 108
2
votes
3 answers

How To Initialize a C Struct using a Struct Pointer and Designated Initialization

How can I use struct pointers with designated initialization? For example, I know how to init the struct using the dot operator and designated initialization like: person per = { .x = 10,.y = 10 }; But If I want to do it with struct pointer? I made…
astro
  • 31
  • 5
1 2 3
8 9