0
0
CComparisonBeginner · 3 min read

Structure vs Union in C: Key Differences and Usage

In C, a 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.

FeatureStructureUnion
Memory AllocationSeparate memory for each memberShared memory for all members
SizeSum of sizes of all membersSize of largest member
Data AccessAll members can be accessed independentlyOnly one member can hold a valid value at a time
Use CaseStore related data togetherSave memory when variables are mutually exclusive
InitializationMembers can be initialized separatelyOnly one member can be initialized at a time
Examplestruct 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

c
#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;
}
Output
x = 10, y = 20
↔️

Union Equivalent

c
#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;
}
Output
d.i = 10 d.f = 220.5
🎯

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.

Key Takeaways

Structures allocate separate memory for each member, allowing simultaneous access.
Unions share memory among members, so only one member holds a valid value at a time.
Use structures for grouping related data that must coexist.
Use unions to save memory when only one of several data types is needed at once.
Accessing union members requires care because they overlap in memory.