LOGBOOK

HELP

Quiz Entry - updated: 2026.07.10

How do you initialize a struct in C?

Either fill fields in order ({10, 20}) or name them with designated initializers ({.x = 10, .y = 20}); any field you omit is zeroed.

struct point { int x, y; };

// Method 1: Positional (order matters!)
struct point p1 = {10, 20};

// Method 2: Designated initializers (C99) - clearer!
struct point p2 = {.x = 10, .y = 20};
// Order doesn't matter
struct point p3 = {.y = 20, .x = 10};

// Method 3: Partial initialization (rest are zeroed)
// y = 0
struct point p4 = {.x = 10};

// Method 4: Zero initialize entire struct
// x = 0, y = 0
struct point p5 = {0};

Nested struct initialization:

struct rect {
    struct point top_left;
    struct point bottom_right;
};

struct rect r = {
    .top_left = {.x = 0, .y = 0},
    .bottom_right = {.x = 100, .y = 100}
};

Array of structs:

struct point points[] = {
    {0, 0},
    {10, 20},
    {30, 40}
};

From Quiz: REVE1 / C Programming | Updated: Jul 10, 2026