Nested Structure in C

 When a structure contains another structure, it is called nested structure. For example,we have two structures named Address and Employee. To make Address nested to Employee, we have to define Address structure before and outside Employee structure and create an object of Address structure inside Employee structure.

Syntax for structure within structure or nested structure

              struct structure1
              {
                     - - - - - - - - - -
                     - - - - - - - - - -
              };

              struct structure2
              {
                     - - - - - - - - - -
                     - - - - - - - - - -
                     struct structure1 obj;
              };

Example for structure within structure or nested structure

  Example 1

 #include<stdio.h>

       struct Address
       {
              char HouseNo[25];
              char City[25];
              char PinCode[25];
       };

       struct Employee
       {
           int Id;
           char Name[25];
           float Salary;
           struct Address Add;
       };

       void main()
       {
              int i;
              struct Employee E;

              printf("\n\tEnter Employee Id : ");
              scanf("%d",&E.Id);

              printf("\n\tEnter Employee Name : ");
              scanf("%s",&E.Name);

              printf("\n\tEnter Employee Salary : ");
              scanf("%f",&E.Salary);

              printf("\n\tEnter Employee House No : ");
              scanf("%s",&E.Add.HouseNo);

              printf("\n\tEnter Employee City : ");
              scanf("%s",&E.Add.City);

              printf("\n\tEnter Employee Pincode : ");
              scanf("%s",&E.Add.PinCode);

              printf("\nDetails of Employees");
              printf("\n\tEmployee Id : %d",E.Id);
              printf("\n\tEmployee Name : %s",E.Name);
              printf("\n\tEmployee Salary : %f",E.Salary);
              printf("\n\tEmployee House No : %s",E.Add.HouseNo);
              printf("\n\tEmployee City : %s",E.Add.City);
              printf("\n\tEmployee Pincode : %s",E.Add.PinCode);

       }
Output 
 

Example 2
#include<stdio.h>
#include<conio.h>
      
struct Date {
    int day;
    int month;
    int year;
};
      struct Student {
    char name[50];
    int rollNumber;
    struct Date birthdate;  // Nested structure
};
        

       int main()
       {
              
              struct Student S;


              printf("\n\tEnter Student Name : ");
              scanf("%s",&S.name);

              printf("\n\tEnter Roll number : ");
              scanf("%d",&S.rollNumber);

              printf("\n\tEnter Day : ");
              scanf("%d",&S.birthdate.day);
              printf("\n\tEnter Month : ");
              scanf("%d",&S.birthdate.month);
              printf("\n\tEnter year : ");
              scanf("%d",&S.birthdate.year);

              printf("\n \t *************Details of Student******************");
              printf("\n\tStudent Name : %s",S.name);
              printf("\n\tStudent Roll number : %d",S.rollNumber);
              printf("\n\tStudent DOB : %d-%d-%d",S.birthdate.day,S.birthdate.month,S.birthdate.year);
             
              
               getch();
              return 0;
             

       }

Output





No comments:

Post a Comment