Structs

Contents

Structs#

A struct is a record with typed, ordered fields. Field access uses . on a value, -> through a pointer.

struct user {
    const char *name;
    int   age;
};

Initialise with designated initialisers; missing fields are zero-initialised.

struct user u = {.name = "rk", .age = 30};
u.age = 31;

Through a pointer, -> is the same as (*p)..

struct user *p = &u;
p->age = 32;

The operator hides struct internals with an opaque type (forward declaration in the header, definition in the source) when the caller should not depend on field layout.

References#

  • Pointers for the * and -> forms.

  • Unions for the overlaid-fields sibling.