C language concept — can a structure be nested inside another structure?\n\nStatement: A structure can be nested inside another structure.

Difficulty: Easy

Correct Answer: Correct

Explanation:


Introduction / Context:
Nested (or composite) aggregates are common in data modeling. This item checks whether C permits struct-within-struct design, which is frequently used for grouping related fields hierarchically.


Given Data / Assumptions:

  • The statement claims a structure may be nested inside another structure.
  • We consider standard, portable C.


Concept / Approach:
Standard C allows a struct member to be another struct type by value. This is a straightforward inclusion where the outer struct contains the entire inner struct as a sub-object. Self-embedding by value is not allowed because it would make the size infinite, but nesting different struct types (or the same via pointers) is fine.


Step-by-Step Solution:

1) Define an inner struct type before using it as a member of the outer struct.2) Example: struct Address { int pin; }; struct Person { struct Address addr; int age; };3) This compiles and produces a Person whose layout includes the full Address sub-object.


Verification / Alternative check:
Compilers handle nested structs routinely; accessing fields uses dot chaining like p.addr.pin.


Why Other Options Are Wrong:

  • Incorrect / Not allowed: Contradict the standard.
  • Only via pointers: Unnecessarily restrictive; by-value nesting is allowed for different types.


Common Pitfalls:
Trying to embed a struct inside itself by value (invalid). Use a pointer for self-reference instead.


Final Answer:
Correct

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion