0
0
CConceptBeginner · 3 min read

What is Designated Initializer in C: Simple Explanation and Example

A designated initializer in C lets you set specific elements of an array or fields of a struct by naming them directly during initialization. This helps you initialize only certain parts clearly without relying on order.
⚙️

How It Works

Think of a designated initializer like filling out a form where you only fill the fields you want, instead of filling every field in order. In C, when you create a struct or array, you can specify exactly which element or member you want to set by name or index.

This means you don’t have to remember the order of fields or elements. You just say, for example, "set the third element to 10" or "set the 'age' field to 25". The compiler then fills in the rest with zeros or default values.

This makes your code easier to read and less error-prone, especially when dealing with large structs or arrays where you only want to set a few values.

💻

Example

This example shows how to use designated initializers to set specific fields in a struct and elements in an array.

c
#include <stdio.h>

struct Person {
    char name[20];
    int age;
    float height;
};

int main() {
    // Initialize only age and height, name will be filled with zeros (empty string)
    struct Person p = {.age = 30, .height = 5.9f};

    // Initialize array with designated initializer
    int numbers[5] = {[2] = 10, [4] = 20};

    printf("Person age: %d, height: %.1f\n", p.age, p.height);
    printf("Array elements: %d %d %d %d %d\n", numbers[0], numbers[1], numbers[2], numbers[3], numbers[4]);
    return 0;
}
Output
Person age: 30, height: 5.9 Array elements: 0 0 10 0 20
🎯

When to Use

Use designated initializers when you want to clearly set only some parts of a struct or array without worrying about the order of fields or elements. This is especially helpful when:

  • You have large structs with many fields but only want to set a few.
  • You want your code to be more readable and maintainable by naming fields explicitly.
  • You want to avoid mistakes from changing the order of fields in the struct definition.
  • You want to initialize sparse arrays where most elements are zero except a few.

For example, in configuration settings or data structures where defaults are common, designated initializers make your intent clear and your code safer.

Key Points

  • Designated initializers let you name fields or array indexes during initialization.
  • They improve code clarity and reduce errors from field order changes.
  • Uninitialized fields or elements default to zero or equivalent.
  • Supported in C99 and later standards.

Key Takeaways

Designated initializers let you set specific struct fields or array elements by name or index.
They make your code easier to read and less error-prone by avoiding reliance on order.
Unspecified fields or elements are automatically set to zero or default values.
Use them when initializing large structs or sparse arrays for clarity and safety.
Designated initializers require C99 or later standard support.