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

Why is designated initialization accepted by gcc with no effect, and generated aggregate is somehow passed as arguments to class constructor

Ok, we have this code: #include using namespace std; class A{ public: A(double b, int g) { cout << "b = " << b << ", g = " << g << endl; } }; int main() { A a = {.g = 5.0, .j = 10}; // output: b = 5, g = 10…
2
votes
3 answers

Can a designated initializer legally refer to the variable it's initializing in C99?

GCC and Clang both allow a designated initializer to refer to a member of the struct or array being initialized, but is this legal and well defined behaviour? The following code example compiles and runs for both GCC and Clang and outputs { .a = 3,…
Andy Stanton
  • 195
  • 1
  • 9
2
votes
3 answers

What is a Designated Initializer?

I wish to understand what designated initializers provide that is different to direct initialization. For example: #include struct Subject{ int x; int y; int z; }; int main() { Subject subject_d{.x = 1, .y = 2, .z=…
rekkalmd
  • 171
  • 1
  • 12
2
votes
1 answer

How do you properly subclass SCNGeometry with your own sources and elements?

Trying to create my own custom SCNGeometry subclass similar to how the framework has SCNBox, SCNPyramid, SCNCapsule, etc. I've already built up my source and element arrays, but the following is a convenience initializer, not a designated…
Mark A. Donohoe
  • 28,442
  • 25
  • 137
  • 286
2
votes
2 answers

gcc compiler report warning but a.out works smoothly otherwise

I am following the book "C Primer Plus" and encounter such a snippet of code: // designate.c -- use designated initializers #include #define MONTHS 12 int main(void) { int days[MONTHS] = {31, 28, [4] = 31, 30, 31, [1] = 29}; int…
AbstProcDo
  • 19,953
  • 19
  • 81
  • 138
2
votes
1 answer

Are non-trivial designated initializers going to be supported in the future (Post C++17)?

This is inspired by this post where non-trivial designated initializers are not supported. I've already read this post and some answers claim this feature is already supported. However, using C++17 and this code: struct s { int a[2]; s (int…
Joseph D.
  • 11,804
  • 3
  • 34
  • 67
2
votes
2 answers

How to declare a subclass designated initializer with more parameters than superclass?

I'm making a subclass designated initializer with an extra parameter compared to the initializer from the superclass, however, i get an error. The code is: class Pregunta: Codable { var codImagen: String var respCorrecta: Int var…
2
votes
2 answers

How to change pointer variables within a struct?

So I am trying to implement a linked list and I have this struct for each of the nodes: typedef struct listNode { struct listNode *next; void *data; } NODE; Previously, I had made a game where the spacecraft struct looked…
DevReacc
  • 230
  • 1
  • 11
2
votes
3 answers

What does this syntax in an initializer list in C mean?

I was reading bootloader code of some OS and came up with such syntaxt: pde_t entry_pgdir[NPDENTRIES] = { // Map VA's [0, 4MB) to PA's [0, 4MB) [0] = ((uintptr_t)entry_pgtable - KERNBASE) + PTE_P, // Map VA's…
Zhani Baramidze
  • 1,407
  • 1
  • 13
  • 32
2
votes
1 answer

Using designated initializers with unnamed nested data types

I'm wondering if it is possible to use designated initializers in unnamed data members of structs... (Yikes, a mouthful, but yes, it is the cleanest way to do what I'm trying to do...). If I have: typedef struct MainStruct { union { …
blackghost
  • 1,730
  • 11
  • 24
2
votes
1 answer

Is it possible for a subclass of SKShapeNode ot use one of its convenience initializers?

I have a class that inherits from SKShapeNode because I want to add additional properties. The problem I am having is that I want to be able to create a "circle" by using SKShapeNode's "convenience" initializer…
malena
  • 798
  • 11
  • 26
2
votes
1 answer

Why isn't the designated initializer being called

According to the docs the designated initializer should always be called, however when I try to get the blood type of user without having the permissions an NSError is created but its designated initializer (- [NSError…
fpg1503
  • 7,492
  • 6
  • 29
  • 49
2
votes
2 answers

Designated Initializer, clarify please.

Now i have had this problem clouding my mind for quite sometime and i really need someone to clarify this for me. 1) how is a designated initializer method determined by the compiler when it is called on by a subclassed init method? as it is said…
2
votes
2 answers

Any caveats using [UIView new];?

Being thought to basically always use the designated initializer I feel a bit dirty when creating new viewInstances with [UIView new]; rather than [[UIView alloc] initWithFrame:CGRectZero];? Is there any reason not to this? Is there any practical…
T. Benjamin Larsen
  • 6,373
  • 4
  • 22
  • 32
2
votes
2 answers

How to initialize void* data struct member with another struct member in C99?

let's assume that we have below struct definitions: typedef struct { uint8_t a ; } deepest_t ; typedef struct { deepest_t* deepest_ptr ; } deeper_t ; typedef struct { deeper_t* deeper_ptr ; } deep_t ; typedef struct { void* data ; }…
Lazureus
  • 462
  • 4
  • 19
1 2 3
8 9