by Dinesh Thakur Category: Structure And Union

We have used the declaration of nested structures on the parent structure in the same declaration. The structures may also be declared separately and included in the parent structure. The innermost structure should be declared first, then the next enveloping structure and then the next enveloping structure. The code is illustrated in Program.

Three structures are declared: The first is Bonus, which has one data member x; the second is Salary, which has two members (one basic pay and other an instance of Bonus); the third is the struct Employee, which has three members namely Name, Age, and Salary. The important thing is how the structure Employee is initialized. In one case, i.e., El, all the data is given like array type initialization; that is, the data values are put in curly brackets and are separated with commas. In case of E2, each data is assigned separately. The following two codes should be understood. In case of Basic_pay there are two selections, ( and in case of x there are three selections (

Illustrates use of typedef with nested structures

# include <stdio.h>

typedef struct
  int x;
} Bonus;
typedef struct
  int Basic_pay;
  Bonus Bon;
} Salary;
typedef struct
  char* Name;
  int Age;
  Salary pay;
} Employee;
void main ()
  Employee E2;
  Employee E1= {"Dinesh", 45, 12000, 2000};
  clrscr(); = 14000; = 3000;
  E2.Age = 25;
  E2.Name = "Shubhra";
  printf(" Name \tAge\tBasic pay\tBonus\n");
  printf ( "%s\t%d\t%d\t\t%d\n", E1.Name, E1.Age,,;
  printf( "%s\t%d\t%d\t\t%d\n", E2.Name, E2.Age,,;

Typedef with Nested Structures in C

About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.C.A, MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.