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

In Objective-C, the rule that designated initializer always get called is not always obeyed?

Can we rely on the fact that in Objective-C, the rule is that a class's designated initializer is always called for sure? Or can we say, it should be almost always true, except a couple of exceptions? For example, for UIView, the docs…
Jeremy L
  • 3,770
  • 6
  • 41
  • 62
3
votes
1 answer

why does this c++ code snippet compile with std=c++17 but fails to compile with std=c++20?

In godbolt: https://godbolt.org/z/bY1a3e1Wz the code in question is below (the error message says "error: either all initializer clauses should be designated or none of them should be" but I dont understand what it is saying.. struct A { int a; …
Palace Chan
  • 8,845
  • 11
  • 41
  • 93
3
votes
1 answer

Initializing std::vector with square brackets [] inside; what is happening?

Background information about what inspired my question: I learned about Designated Initializers in C (see here and here: https://gcc.gnu.org/onlinedocs/gcc/Designated-Inits.html), which are awesome, and allow you to initialize C arrays in C like…
Gabriel Staples
  • 36,492
  • 15
  • 194
  • 265
3
votes
0 answers

How to use designated initializers in structs inherited from another struct in C++20?

int main() { struct TStruct1 { int Field1; int Field2; }; struct TStruct2 : TStruct1 { int Tag; }; TStruct2 t2{ { .Field1 = 1, .Field2 = 2, }, .Tag = 3 …
vladon
  • 8,158
  • 2
  • 47
  • 91
3
votes
0 answers

If "designated initialization" is C++20 feature, why is the following code compiled with the given options?

AFAIK, "designated initialization" is a C++20 feature(ref: https://en.cppreference.com/w/cpp/language/aggregate_initialization). However, the following code, // main.cc #include struct Person { const char *name; int age; }; int…
Hakkyu Kim
  • 43
  • 3
3
votes
3 answers

Why is address operator (&) needed when creating struct?

I have some code that works, but I don't understand why. Here is the code in question: struct SceneInterface *TitleAsScene = &(struct SceneInterface) { .update = (void (*)(void *)) title_update, .render = (void (*)(void *))…
user1801359
  • 422
  • 1
  • 4
  • 14
3
votes
2 answers

What's this type of initialization called?

I saw code in a Vulkan tutorial that goes: VkImageMemoryBarrier imageMemoryBarrier = { .srcAccessMask = VK_ACCESS_SHADER_WRITE_BIT, .dstAccessMask = VK_ACCESS_SHADER_READ_BIT, .oldLayout = VK_IMAGE_LAYOUT_GENERAL, .newLayout =…
3
votes
2 answers

Why can't a defined, fix-sized array be assigned using a compound literal?

Why is it so that a struct can be assigned after defining it using a compound literal (case b) in sample code), while an array cannot (case c))? I understand that case a) does not work as at that point compiler has no clue of the memory layout on…
davidanderle
  • 638
  • 6
  • 12
3
votes
1 answer

Designated Initializer Array in C

I'm trying to initialize an struct array using designated initializer: A[size].from = {[0 ... size - 1] = 0}; A[size].to = {[0 ... size - 1] = 0}; List.h: typedef struct Buffer { int from; int to; }buffer_t; typedef struct…
Zam-Oxy
  • 130
  • 10
3
votes
2 answers

C++20 Designated Initializers char[]

C++20 introduces designated initialisers. I'm trying to initalise a character array inside a struct with the new syntax. Pretty simple problem, I think code may explain it best: struct Test { unsigned v; char b[12]; }; int main() { char…
3
votes
2 answers

Using a designated initializer to initialize nested struct with a `char []` member

I have the following nested struct definition: typedef struct { int count; float cash; char item[50];//switch between array and pointer for testing initializers //char *item; }Purchase; typedef struct { int accnt; char…
ryyker
  • 22,849
  • 3
  • 43
  • 87
3
votes
1 answer

Are there any pitfalls to simulate defaulted function parameters using aggregate initialization?

Question: If you want, for example, a variadic function that takes arbitrary number of parameter Args&&...args, and prints all those arguments t times. What's more is that you want t to be defaulted to 1, so it prints all args one time by…
ph3rin
  • 4,426
  • 1
  • 18
  • 42
3
votes
1 answer

Initialize array of strings indexed by enumeration in C

I am hoping to make an array of strings based on an enumeration in C programming language. Ideally I would like to declare this as a constant, so I would like to declare it at compile time rather than fill it in during my program execution. As an…
Beau R
  • 53
  • 5
3
votes
1 answer

Why can't I initialize an array within a nested structure?

I want to create a new structure that is the combination of two identical structures. Each of these sub structures includes one scalar double and a three dimensional array. However, when I try to initialize the structure I am getting errors when…
jdhabez
  • 41
  • 4
3
votes
2 answers

Initializing an array of pointers to structs in C++

Initializing an array of pointers to structs in C can be done using compound literals. typedef struct { int a; int b; } s; In C: s *ptrArray[] = { &(s){ .a = 1, .b = 2 }, &(s){ .a = 4, .b = 5 …
vg34
  • 91
  • 2
  • 10
1 2 3
8 9