Skip to main content
A nested struct in C is a composite data type where a struct contains one or more struct variables as its members. This creates a hierarchical data model where the inner structure is fully encapsulated within the memory footprint of the outer structure, resulting in a single contiguous block of memory.

Declaration Syntax

There are two primary methods to define a nested struct: independent declaration and embedded declaration. 1. Independent Declaration (Preferred) The inner struct is defined globally or in the same scope prior to the outer struct. This allows the inner struct type to be reused elsewhere.
2. Embedded Declaration The inner struct is defined directly within the outer struct. If the inner struct lacks a tag (anonymous struct type), it cannot be instantiated outside of the outer struct.
Note: C11 introduced true anonymous structs, allowing members of an untagged, unnamed nested struct to be accessed as if they were direct members of the outer struct.

Initialization

Nested structs are initialized using nested brace-enclosed initializer lists. Modern C (C99 and later) supports designated initializers, which provide strict mapping to the nested members.

Member Access

Accessing nested members requires chaining the member access operators. The dot operator (.) is used for direct value access, while the arrow operator (->) is used when resolving through a pointer.

Memory Layout and Alignment

A nested struct does not store a pointer to the inner struct; it expands the inner struct’s members inline. The memory allocation is strictly contiguous. The compiler applies standard Application Binary Interface (ABI) alignment and padding rules to both the inner and outer structs. The alignment requirement of the outer struct is determined by the largest alignment requirement among all its members, including the nested struct (which itself is aligned based on its largest member).
Tired of Poor C Skills? Fix That With Deep Grasping!Learn More