Govt Exams
Passing by pointer avoids copying the entire structure, making it efficient for large data structures.
typedef creates an alias for the struct type, allowing direct use without 'struct' keyword.
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.
struct Point { int x; int y; };
struct Point p = {5};
printf("%d %d", p.x, p.y);
When initializing a struct with fewer values than members, remaining members are zero-initialized. p.x=5, p.y=0.
Designated initializers (C99) allow specifying members by name using dot notation, making code more readable and maintainable.
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.
ptr points to obj, so ptr->a = 10 modifies obj.a to 10. The printf outputs the modified value 10.
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.
In 'struct person { int age; } p;', 'person' is the tag (structure name) and 'p' is the type/variable. Tag names the structure template; type creates actual instances.