STRUCTURE INSIDE THE STRUCTURE OR NESTED STRUCTURE:
  • Structure within structure means nesting of structure.
  • If we declare a structure inside another structure, then it is referred to as nested structure.
  • In another words, when a structure is a member of another structure, it is called a nested structure.
  • In following example, we have defined two structures: Address and Student.
struct address
{
   char society[100];
   char area[100];
   char city[50];
   int pcode;
};

struct student
{
   int rollno;
   char name[50];
   struct address add;		// Structure inside structure
};
  • In above example we have defined one structure having member variables society, area, city, and pcode and another structure having member variables roll number, name, and one structure variable of type structure “address”.
  • We can also define above structure by following way. Any method is valid for C program.
struct student
{
    int rollno;
    char name[50];  // Structure address defined inside the structure
     
    struct address
    {
         char society[100];
         char area[100];
         char city[50];
         int pincode;
     }add;
};
  • We can access the members of nested structure in program as under:
void main()
{
    struct student s;
     
    printf(“Enter student detail\n”);
    scanf(“%d”, &s.rollno);
    gets(s.name);
     
    // Accessing the member of nested structure
    gets(s.add.society);
    gets(s.add.area);
    gets(s.add.city);
    scanf(“%d”,&s.add.pincode);
}
  • The C99 standard specifies that at least 63 levels of nesting can be allowed.
// Write a C program to read and display information of an employee, using a nested structure.

#include “stdio.h”
typedef struct
{
     int day;
     int month;
     int year;
}DOJ;   // DOJ = Date of joining

typedef struct
{
     char name[30];
     int ID;
     DOJ doj;
     float salary;
}EMPLOYEE;

void main( )
{
     int n,i;
     printf(“\n Enter the number of Employees: ”);
     scanf(“%d”,&n);

     EMPLOYEE emp[n]; // Array of structures declaration.
     for(i=0;i<n; i++)
     {
        printf(“\n Enter %d Employee details: ”);
        printf(“\n\t Enter Employee Name: ”);
        scanf(“%s”,emp[i].name);
        
        printf(“\n\t Enter Employee ID: ”);
        scanf(“%d”,&emp[i].ID);
        printf(“\n\t Enter Employee Date of Joining: ”);
        scanf(“%d %d %d”, &emp[i].doj.day, &emp[i].doj.month,
        &emp[i].doj.year); 
       
        // Nested Structure 
        printf(“\n\t Enter Employee Salary: ”); 
        scanf(“%f”,&emp[i].salary);
     }
     printf(“\n ********** STORED INFORMATION **********”);

     for( i=0 ; i<n;i++)
     {
        printf(“Details of %d Employee: ”);
        printf(“\n\t Employee Name = %s”,emp[i].name);
        printf(“\n\t Employee ID = %d”,emp[i].ID);
        printf(“\n\t Employee Date of Joining = %d-%d-%d”,
        emp[i].doj.day,emp[i].doj.month,emp[i].doj.year);
        printf(“\n\t Em ployee Salary =%f ”,emp[i].salary); 
     } 
}

OUTPUT:

Enter the number of Employees: 2
Enter 1 Employee details:
Enter Employee Name: Sameer Shekh
Enter Employee ID: 10102
Enter Employee Date of Joining: 1 6 2013 
Enter Employee Salary: 40000    //User Input
Enter 2 Employee details:
Enter Employee Name: Aryan Kumar
Enter Employee ID: 10125
Enter Employee Date of Joining: 3 6 2013 
Enter Employee Salary: 45000 
********** STORED INFORMATION **********
Details of 1 Employee:
Employee Name = Sameer Shekh
Employee ID = 10102
Employee Date of Joining = 1 – 6 – 2013 
Employee Salary = 40000    // Program Output
Details of 2 Employee:
Employee Name = Aryan Kumar
Employee ID = 10125
Employee Date of Joining = 3 – 6 – 2013 
Employee Salary = 45000