Govt. Exams
Entrance Exams
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.
Bit field allocation order (left-to-right or right-to-left) varies across compilers, affecting portability.
union Data { int x; char y; };
Data d = {65};
d.y = 'A';
printf("%d", d.x);
Modifying y changes the same memory location as x. Only the last byte of x is affected, leading to unpredictable output.
Passing by pointer avoids copying the entire structure, making it efficient for large data structures.
struct Data { char c; int i; short s; };
Due to padding/alignment: char(1) + padding(3) + int(4) + short(2) = 12 bytes on 32-bit systems.