Module 4: Structs, Unions & Bit Fields
Laying out data exactly as you want it in memory
Lessons
| ID | Title | Duration |
|---|---|---|
| 4-1 | Structs: Layout, Padding & Alignment | 30 min |
| 4-2 | Unions and Type Punning | 25 min |
| 4-3 | Bit Fields | 20 min |
| 4-4 | Linked Lists and Trees | 35 min |
| 4-5 | Struct-Based OOP in C | 25 min |
Project
| 4-proj | Project: Free-List Allocator | 75 min |
Key Concepts
structs, unions, pointers, malloc
PDF Status
- PDF ch05 (Structs, Unions, and Bit Fields) — Complete (written 2026-04-08).
- Key Concepts: alignment table, padding rules (with step-by-step layout for four example structs), flexible array members, unions, bit fields, designated initialisers, compound literals.
- Going Deeper: full alignment/padding derivation,
__attribute__((packed))costs, union type punning (C11 §6.5.2.3 + memcpy alternative), struct layout across platforms (32/64-bit, portable serialisation with htonl/ntohl), designated initialisers in Linux kernel init_task. - Real-World Connection: Linux kernel
include/linux/if_ether.h—ethhdrpacked struct mapping Ethernet frame bytes,__be16sparse annotation,get_unaligned_be16()for ARM safety. - Exercises 5.1–5.4 + fully annotated solutions + test files.