Module 4: Structs, Unions & Bit Fields

Laying out data exactly as you want it in memory

Lessons

IDTitleDuration
4-1Structs: Layout, Padding & Alignment30 min
4-2Unions and Type Punning25 min
4-3Bit Fields20 min
4-4Linked Lists and Trees35 min
4-5Struct-Based OOP in C25 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.hethhdr packed struct mapping Ethernet frame bytes, __be16 sparse annotation, get_unaligned_be16() for ARM safety.
    • Exercises 5.1–5.4 + fully annotated solutions + test files.