Govt Exams
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.
struct S { int a:3; int b:3; int c:3; };
printf("%zu", sizeof(struct S));
Bit field packing is implementation-defined. Size depends on compiler's bit field allocation strategy.
In C, self-referential structures require the struct keyword in pointer declarations within the structure definition.
All union members share the same memory. Accessing a member not recently written gives bitwise interpretation of that memory.
struct FlexArray { int len; int arr[]; };
Flexible array members require dynamic allocation; the structure itself cannot be allocated on the stack with a defined size for the array.
Bit field allocation order (left-to-right or right-to-left) varies across compilers, affecting portability.
Both methods are valid - A uses named struct tag with variable declaration, C uses anonymous struct with variable declaration.
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.
Union size equals the largest member size. float is typically 4 bytes, which is largest.