Govt Exams
#pragma pack(1) removes padding, making members occupy consecutive memory locations for compact storage.
struct X { int a; float b; };
struct Y { struct X x; int c; };
printf("%zu", sizeof(struct Y));
Nested struct X: int(4) + float(4) = 8 bytes. Y: struct X(8) + padding(0) + int(4) = 12, but with overall alignment it becomes 16 bytes.
Unions are optimal for memory-constrained scenarios and hardware programming where type casting is needed.
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.
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.
Compilers add padding bytes between structure members to align them on boundaries (usually power of 2), improving memory access performance on the target architecture.
Both o->ptr->x and (*o).ptr->x are equivalent. Arrow operator can chain for pointers, and (*pointer).member is equivalent to pointer->member.
Bit fields can pack 8 flags per byte, so 100 flags need ~13 bytes. Char array needs 100 bytes. Bit fields are more memory-efficient for flag storage.
Since union members share memory, setting y[0]=65 and y[1]=66 overwrites the int x value. The exact value depends on endianness.
Self-referential structures contain pointers to the same structure type (like linked list nodes). Recursion refers to function calls, not structures.