Loading…
Loading…
Charting your lesson…
Opening your lesson…
Structures, Unions and Enums
Lesson 4 of 4
6 steps•about 12 min•11 flashcards
Step 1/ 61 min
When one value is not enough, C gives you three ways to package data: struct (all members together), union (members overlapping), and enum (named integer constants). Choosing among them is a memory-model decision.
Core idea: a struct gives each member its own storage laid out in order (so its size is the sum, plus padding); a union overlays all members in the same storage (so its size is the largest member, and only one is valid at a time); an enum is just readable names for int values.
Step 2/ 61 min
struct Point { int x; int y; };
struct Point p = {3, 4};
p.x = 5; // dot for a value; p->x for a pointer
Members are stored in declaration order. sizeof(struct Point) is at least the sum of member sizes.
The compiler inserts padding so each member sits at an address that is a multiple of its alignment (an int typically aligns to 4, a double to 8). This makes sizeof larger than the naive sum:
struct S { char c; int i; }; // sizeof is 8, not 5:
// [c][pad][pad][pad][i i i i] — 3 bytes padding after c so i is 4-aligned
Why: unaligned access is slow or illegal on some CPUs. Ordering members largest-to-smallest often shrinks a struct by reducing padding.
union Value { int i; float f; char bytes[4]; };
union Value v;
v.i = 1; // writing i and then reading f is meaningless
All members share the same 4 bytes, so sizeof(union Value) is 4 (the largest member). Unions save memory when a value is "one of several types" — often paired with an enum tag that records which member is currently live (a "tagged union"). enum constants default to 0, 1, 2… but can be assigned: enum Level { LOW=1, MID, HIGH }; gives 1, 2, 3.
Step 3/ 61 min
enum Kind { INT_K, FLT_K };
struct Var {
enum Kind kind; // which member is valid
union { int i; float f; } data;
};
struct Var a = { INT_K, .data.i = 42 };
if (a.kind == INT_K) printf("%d\n", a.data.i); // 42
The enum tag tells safe code which union member to read. Reading a.data.f here would reinterpret the int's bits as a float — a bug the tag exists to prevent.
Step 4/ 61 min
Very common: "compute sizeof of a struct with padding", "difference between struct and union" (separate vs shared storage → size = sum vs size = max), and "what is a tagged union / when to use a union". Structure padding questions test whether you truly understand memory layout.
Step 5/ 61 min
. on a struct value, -> on a struct pointer (p->x is (*p).x).Assuming sizeof(struct) equals the sum of its members' sizes. Alignment padding almost always makes it larger, and the exact value depends on member order. Writing serialization code that trusts the naive sum will read or write the wrong number of bytes.
struct: members have separate storage in order; size ≈ sum + padding.union: members overlap; size = largest member; only one valid at a time.enum names integer constants (default 0,1,2…; assignable).enum tag + union payload for type-safe variants.struct when you need every field at once (pay padding), union when only one field is live at a time (save memory, track it with an enum tag), and enum to give integers meaningful names.Step 6/ 6· Worked examples2 min
Problem
On a 64-bit system with 4-byte int and 8-byte alignment, compute sizeof(struct S) and sizeof(union U), and explain padding.
struct S { char a; int b; char c; }; // struct
union U { char a; int b; double d; }; // union
enum Color { RED, GREEN = 5, BLUE }; // enum values?
Try it on paper first.
Recall
Keep this
Structures, unions, and enums test whether you understand memory layout — especially struct padding and the fact that a union shares one block of memory. These are favourite "what is sizeof?" trick questions in placement written tests.
A struct groups different types into one record; each member has its own storage, laid out in declaration order. The compiler inserts padding so members meet alignment requirements, so sizeof(struct) is often larger than the sum of member sizes.
A union gives all members the same starting address — they overlap and share one block. Its size equals the largest member. Writing one member and reading another reinterprets the bytes (useful for type-punning, dangerous if misused). Only one member is meaningfully "active" at a time.
An enum is a set of named integer constants, starting at 0 by default and incrementing by 1 unless assigned. It improves readability over magic numbers.
| Feature | struct | union |
|---|---|---|
| Memory | sum + padding | size of largest member |
| Members active | all at once | one at a time |
| Use | records | space-saving / type-punning |
sizeof(struct) ≠ sum of members due to padding/alignment — the total is rounded up to the largest member's alignment.union's size is the size of its biggest member; all members share address 0 of the block.enum values start at 0 and auto-increment; assigning one (enum {A=5, B, C} → B is 6) shifts the rest.-> to access members through a struct pointer, . for a struct object.sizeof of a struct equals the sum of its fields, forgetting padding."Compute sizeof of this struct" (padding), "what does the union print after writing member A and reading member B?", enum auto-increment values, and . vs -> selection.
struct = separate storage per member + padding; union = shared memory sized to the largest member (one active at a time); enum = auto-incrementing named integers from 0. Reorder struct fields to cut padding; use -> on pointers.
You've read 0 of 6 steps
Now lock it in — questions straight after reading are what make it stick.