Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

Struct initialization in C is the process of binding initial values to the contiguous memory locations representing the members of a user-defined struct type at the point of variable declaration.

Ordered Initialization (C89)

The traditional method assigns values to struct members strictly in the order they are defined. The initializer list is enclosed in braces {} and separated by commas.
struct Point {
    int x;
    int y;
    float z;
};

/* Values map to x, y, and z sequentially */
struct Point p1 = {10, 20, 5.5f};

Designated Initialization (C99)

Designated initializers use the dot operator (.) to explicitly name the members being initialized. This approach is order-independent, allowing you to initialize members in any sequence.
struct Configuration {
    int baud_rate;
    int parity;
    int stop_bits;
};

/* Order is independent of struct definition */
struct Configuration cfg = {
    .stop_bits = 1,
    .baud_rate = 9600
};

Partial Initialization and Zero-Padding

If an initializer list provides fewer values than the struct has members, the compiler implicitly initializes all remaining unlisted members to zero (integer 0, floating-point 0.0, or NULL for pointers).
struct Buffer {
    int id;
    char data[256];
    int *ptr;
};

/* id = 1, data array is all '\0', ptr = NULL */
struct Buffer buf1 = {1};

/* Idiomatic zero-initialization of the entire struct */
struct Buffer buf2 = {0}; 

Nested Struct Initialization

When a struct contains another struct or an array, the initializer lists can be nested using additional braces. In C99, designated initializers can be chained to reach deep members directly.
struct Transform {
    struct Point position;
    struct Point rotation;
};

/* Ordered nested initialization */
struct Transform t1 = {{0, 0, 0.0f}, {90, 0, 0.0f}};

/* Designated nested initialization */
struct Transform t2 = {
    .position = {.x = 0, .y = 0, .z = 0.0f},
    .rotation.x = 90 /* Chained designator */
};

Compound Literals (C99)

Compound literals create an unnamed struct object in memory. While technically an expression rather than a declaration initializer, they are used to initialize struct pointers or assign complete sets of values to existing struct variables post-declaration.
struct Point p2;

/* Assignment using a compound literal */
p2 = (struct Point){.x = 5, .y = 10, .z = 1.0f};

/* Initializing a pointer to an anonymous struct */
struct Point *p_ptr = &(struct Point){1, 2, 3.0f};

Storage Duration Rules

The initial state of a struct depends on its storage duration if no explicit initializer is provided:
  • Automatic Storage Duration: Local structs declared without an initializer contain indeterminate (garbage) values. Reading them before assignment invokes undefined behavior.
  • Static/Thread Storage Duration: Global or static structs declared without an initializer are guaranteed by the C standard to be implicitly zero-initialized.
Master C with Deep Grasping Methodology!Learn More