Entrance Exams
Govt. Exams
A union is the appropriate choice when you need to store different data types but only one value at a time.
Unions are primarily used in embedded systems to conserve memory by allowing multiple members to share the same memory location.
Both nested structure definitions (defining B inside A and declaring B as member) are valid in C.
In C, you can assign one structure variable to another of the same type, and all members are copied (shallow copy).
Self-referential pointers (a structure containing a pointer to itself) are essential for creating linked list nodes.
struct Point { char c; int x; };
Due to structure padding/alignment, char (1 byte) + 3 bytes padding + int (4 bytes) = 8 bytes total.
Unions enable type punning and efficient hardware register representation where different interpretations of same memory are needed.
Bit fields efficiently store boolean/limited-range states using minimal memory, ideal for state machines.
struct Address { char city[20]; };
struct Person { Address addr; };
Person p;
p is a structure variable (not pointer), so arrow operator (->) cannot be used. Only dot operator works.
All union members share the same memory. Accessing a member not recently written gives bitwise interpretation of that memory.