Structures and Unions in C: quick revision, 20 bites
All 20 revision bites from this kit, free and with no sign-up.
Core Difference in One Line
Structs side-by-side, unions overlap.
A **struct** stores all members **side-by-side**, each with its own memory. A **union** stores all members in the **same memory space**, so only one member is valid at a time.
Takeaway. Structs keep all data accessible; unions save memory by sharing space.
What is a Structure?
Bundle different types into one.
A **structure** (`struct`) is a user-defined data type that groups **different types** of variables under one name. All members exist at the same time, each with its own memory location.
Takeaway. A struct is a collection of related but distinct data fields.
What is a Union?
Different types, same memory spot.
A **union** (`union`) allows storing **different types** in the same memory location. Only **one member is active at a time**; all members share the same starting address.
Takeaway. A union is a single memory space that can hold one of several types.
Memory Size: Struct vs Union
Sum vs largest member.
Takeaway. Structs use more memory but keep all data; unions use less but only one value at a time.
Struct Example in Action
Student record with all fields.
Takeaway. In a struct, you can read and write all members independently.
Union Example in Action
One value at a time.
Takeaway. Writing to one union member overwrites the others.
Toolbox vs Single Drawer
Separate slots vs one drawer.
A **struct** is like a **toolbox** with separate compartments — every tool has its own slot. A **union** is like a **single drawer** — put in a hammer, the wrench is gone.
Takeaway. Structs keep all tools; unions hold only one tool at a time.
When to Use Struct
Keep all fields together.
Use `struct` when you need to keep all fields together and access them at the same time. Examples include a date (day, month, year), a student record, or a point (x, y).
Takeaway. Structs are for grouping related data you need simultaneously.
When to Use Union
One of several types.
Use `union` when you have a variable that can hold **one of several types** but never more than one at once. Examples include a token in a parser (number, string, or operator) or a network packet payload.
Takeaway. Unions are for memory-efficient variants where only one type is active.
Reading the Wrong Union Member
Don't read inactive members.
Takeaway. Always track which union member is currently active.
Tagged Unions
Add a flag to track type.
To safely use a union, pair it with a **tag** (an enum or int) that indicates which member is active. This is called a *tagged union* or *discriminated union*.
Takeaway. Always use a tag to know which union member is valid.
Size Calculation Formula
Sum vs max.
Takeaway. Struct size is sum plus padding; union size is the largest member.
Quick Size Comparison
See the size difference.
Takeaway. Unions can save significant memory when only one field is used at a time.
Real-World: Network Packets
Unions in packet parsing.
Network protocols often use unions to represent packet headers that can be of different types (e.g., TCP, UDP, ICMP). The union holds the specific header type, saving memory and allowing flexible parsing.
Takeaway. Unions are used in systems programming for memory-efficient variant handling.
Real-World: Compiler Tokens
Unions in lexical analysis.
In compilers, a token can be a number, string, or operator. A union stores the token's value, while a separate tag indicates the token type. This is a classic *tagged union* pattern.
Takeaway. Tagged unions are essential for implementing variants in C.
Exam Tip: Size Questions
Remember sum vs max.
In exams, when asked for the size of a struct, add up all members and consider padding. For a union, just take the largest member's size. Don't forget padding for structs!
Takeaway. For size questions: struct = sum + padding, union = max.
Memory Trick: S for Separate, U for United
S = separate, U = united.
**S**truct = **S**eparate memory for each member. **U**nion = **U**nited (shared) memory. This helps recall the fundamental difference.
Takeaway. S for separate, U for united — easy to remember.
Did You Know?
Unions can save memory.
Unions are often used in embedded systems where memory is limited. By sharing memory, they allow a single variable to hold different data types without wasting space.
Takeaway. Unions are a memory-saving tool in resource-constrained environments.
How to Choose: Struct or Union?
Ask: need all at once?
Takeaway. Choose struct for simultaneous access, union for memory-efficient variants.
Warning: Overwriting in Unions
Writing overwrites other members.
Takeaway. Unions are not for storing multiple values simultaneously.
Your notes can be a kit like these
Every kit here was made from real study material. Point Veda at a chapter, a PDF, a photo or a link and get notes, flashcards, practice MCQs, games and a scored test — free, in 42 languages.
- Chapter notes
- Flashcards
- Practice MCQs
- Scored tests
- Exam prep
Get Veda free See what students get
Free to start · No ads · Works in 42 languages