Getting Started With C

In C programming, a nested structure refers to a structure that is defined within another structure. This allows you to create a hierarchical or layered organization of data. There are two main types of nested structures in

1)By separate nested structure
Syntax: struct Inner { // Inner structure members int innerVar; }; struct Outer { // Outer structure members int outerVar; struct Inner nestedStruct; // Member structure };
2)By embedded nested structure
Syntax: struct Outer { // Outer structure members int outerVar; struct Inner { // Inner structure members int innerVar; } nestedStruct; // Member structure };

Explanation

Outer structure contains an instance of the Inner structure as a member. 
Accessing members: outerInstance.nestedStruct.innerVar
  • Examples :

    Program.c


  •         

    Output

    Outer Variable: 10 Inner Variable: 20

    Resources :

    Test Your Knowledge
    Choose The

    With supporting text below as a natural lead-in to additional content.

    Q1. If you have a nested structure named Car inside another structure named Owner, how would you declare a variable named myCar of type struct Owner?

    Q2. Suppose you have a nested structure named Address inside another structure named Person. How would you initialize the street member of the address structure for a variable named someone of type struct Person?