← Structs and UnionsC PROGRAMMING 1.4.C

Nested & Advanced Structs

Structs containing structs, and the layout implications.

StatusCollected
NotesChapter test: 5/5

Structs containing structs, and what that does to the actual memory layout underneath.

struct address {
    char city[32];
    int zip;
};

struct person {
    char name[32];
    struct address addr;   /* embedded, not a pointer — laid out inline */
};

struct person p;
p.addr.zip = 12345;

addr here isn't a pointer to a separate allocation — it's laid out directly inside person, contiguous with the other members. sizeof(struct person) includes the full address inline, and p.addr.zip chains the dot operator straight through both levels.

Notes from readers

Comments — via GitHub