Structures and Unions

Lesson 13 of 20

Group related values into one record — and see why a union makes all its members share a single block of memory.

The big picture

One name, many values

A struct bundles related values into one named record; a union lets several members share the same bytes.

One box, many types

A struct groups values of different types — name, score, health — under a single variable name.

Members live side by side

Each struct member gets its own memory; changing one never touches the others.

Dot and arrow

Use p.score for a struct variable, and p->score when p is a pointer to a struct.

Union = shared memory

A union keeps all members at one address — writing one member overwrites the others.