How to use structs

Define a structure

A structure type groups fields that may have different types.

struct Point {
    double x;
    double y;
};

struct Point origin = {0.0, 0.0};

Use designated initializers

Designated fields make initialization readable and tolerate field reordering better than positional initializers.

struct Student {
    char name[32];
    int score;
};

struct Student student = {.name = "Ada", .score = 95};

Access values and pointers

Use . with a struct object and -> with a valid pointer to one.

void move_right(struct Point *point)
{
    if (point != NULL) {
        point->x += 1.0;
    }
}

Copy with care

Assigning one struct copies its fields, including embedded arrays. Pointer fields copy only addresses, so ownership is not duplicated automatically.