C Structures & Unions - Tricky MCQ

Previous C Structures & Unions Next

Tricky Questions on Structures & Unions

1

What is the fundamental difference between structure and union?

Correct Answer: D) All of the above

Structure allocates separate memory for each member (all can hold values simultaneously). Union allocates shared memory (only one member valid at a time). Structure size ≥ sum of members (due to padding), union size ≥ largest member.

2

What is structure padding and why does it occur?

Correct Answer: D) All of the above

Padding ensures members are aligned to their natural boundaries (int on 4-byte boundary, short on 2-byte). Unaligned access is slower or causes hardware exceptions on some architectures. Structure size often larger than member sum due to padding.

3

What is the size of empty structure in C?

Correct Answer: B) 1 byte (minimum size for existence)

Empty structures are allowed in C but have size 1 byte to ensure each instance has unique address. Needed for array indexing and pointer arithmetic. In C++, empty classes also have size 1 for same reason. Practical use is limited.

4

What is bit field in structures?

Correct Answer: D) All of the above

Bit fields allow specifying exact bit width for integer members. Useful for packing multiple flags into single word. Behavior is implementation-defined: bit ordering (LSB/MSB), padding between fields, maximum width. Not portable across compilers.

5

What is flexible array member in structures (C99)?

Correct Answer: D) All of the above

Flexible array member declared as array[] at end of struct. Allows variable-sized structures. Must allocate with malloc(sizeof(struct) + n*sizeof(type)). Standardized in C99, replacing non-portable struct hack with array[1]. Must be last member, struct must have other members.

6

What is anonymous union/structure (C11)?

Correct Answer: D) All of the above

Anonymous unions/structures have no tag name, declared inside another struct/union. Their members become members of containing struct. Useful for variant types or accessing same memory with different interpretations. C11 feature, previously compiler extensions.

7

What is designated initializer for structures?

Correct Answer: D) All of the above

Designated initializers use .member = value syntax. Members can be initialized in any order. Unspecified members default to zero (or NULL for pointers). Makes code more readable and maintainable, especially with large structures. Available for arrays too.

8

What is structure assignment in C?

Correct Answer: D) All of the above

Structures can be assigned using = operator (member-wise copy). For pointer members, only pointer value copied, not dereferenced data (shallow copy). Structures can be passed/returned by value (copied). Large structures should be passed by pointer for efficiency.

9

What is the difference between . and -> operators?

Correct Answer: D) All of the above

Dot operator (.) accesses members of structure/union variables. Arrow operator (->) dereferences pointer and accesses member. They're syntactically different but semantically related: ptr->member equals (*ptr).member. Arrow has higher precedence than dereference.

10

What is packed structure?

Correct Answer: D) All of the above

Packed structure eliminates padding between members. Size equals sum of member sizes. Implemented via compiler extensions: __attribute__((packed)) in GCC, #pragma pack in MSVC. Causes unaligned memory access, slower on some architectures, may cause hardware faults on others.

11

What is union type punning and is it safe?

Correct Answer: D) All of the above

Type punning via unions (reading different member than last written) is well-defined in C99. In C89, it's undefined behavior. Used for interpreting same bits as different types (float/int conversion, protocol parsing). Safer than pointer casting which violates strict aliasing.

12

What is forward declaration of structure?

Correct Answer: D) All of the above

Forward declaration declares structure exists without defining members. Creates incomplete type - can declare pointers but not variables. Essential for self-referential structures (linked lists, trees) and mutual dependencies between structures. Must complete definition before using members.

13

What is structure comparison in C?

Correct Answer: D) All of the above

C doesn't provide structure comparison operators. Must write custom function comparing each member. memcmp() unreliable because padding bytes may contain garbage. For unions, compare active member (need to track which is active).

14

What is tagged union (discriminated union)?

Correct Answer: D) All of the above

Tagged union combines union with discriminator field (usually enum). Tag indicates which union member is currently valid. Essential for type-safe variant handling. Used in compilers (AST nodes), protocol parsing, and any situation needing multiple data types in single variable.

15

What is offsetof macro?

Correct Answer: D) All of the above

offsetof(type, member) returns offset in bytes from start of structure to specified member. Includes any padding before member. Used in low-level code, generic containers, and Linux kernel's container_of macro. Implementation typically uses null pointer arithmetic.

Previous C Structures & Unions Next