Structure vs Union in C: Key Differences and Usage
structure stores multiple variables in separate memory locations allowing all members to hold values simultaneously, while a union shares the same memory for all members, so only one member can hold a value at a time. Structures are used when you need to keep all data together, and unions are useful to save memory when variables are mutually exclusive.Quick Comparison
Here is a quick side-by-side comparison of structure and union in C.
| Feature | Structure | Union |
|---|---|---|
| Memory Allocation | Separate memory for each member | Shared memory for all members |
| Size | Sum of sizes of all members | Size of largest member |
| Data Access | All members can be accessed independently | Only one member can hold a valid value at a time |
| Use Case | Store related data together | Save memory when variables are mutually exclusive |
| Initialization | Members can be initialized separately | Only one member can be initialized at a time |
| Example | struct Point { int x; int y; }; | union Data { int i; float f; char str[20]; }; |
Key Differences
A structure in C is a collection of variables (called members) grouped under one name. Each member has its own memory location, so all members can store values simultaneously. This makes structures ideal for representing complex data where all parts are needed at once, like a point with x and y coordinates.
On the other hand, a union also groups variables under one name, but all members share the same memory space. This means only one member can hold a meaningful value at any time. The size of a union is equal to its largest member. Unions are useful when you want to store different types of data in the same memory location but never at the same time, saving memory.
Accessing members in a structure is straightforward and safe because each member has its own space. In unions, changing one member affects the others because they overlap in memory, so you must be careful to use only the active member. This difference affects how you design your program and manage data.
Code Comparison
#include <stdio.h>
struct Point {
int x;
int y;
};
int main() {
struct Point p = {10, 20};
printf("x = %d, y = %d\n", p.x, p.y);
return 0;
}Union Equivalent
#include <stdio.h>
union Data {
int i;
float f;
char str[20];
};
int main() {
union Data d;
d.i = 10;
printf("d.i = %d\n", d.i);
d.f = 220.5f;
printf("d.f = %.1f\n", d.f);
// Note: d.i now holds garbage because memory is shared
return 0;
}When to Use Which
Choose structure when you need to store multiple related data items together and access them independently, like storing a person's name, age, and height all at once. Structures are best when all data members are important simultaneously.
Choose union when you want to save memory by storing different types of data in the same space but only one at a time, such as a variable that can hold either an integer or a float depending on context. Unions are ideal for memory optimization when data types are mutually exclusive.