Structure within Structure : Nested Structure

  • Structure written inside another structure is called as nesting of two structures
  • A structure can be the member of another structure
Way 1 : Declare two separate structures
struct date
{
   int date;
   int month;
   int year; 
};
struct Employee
{
   char ename[20];
   int ssn;
   float salary;
   struct date doj;
}emp1;
Accessing Nested Elements :
  1. As we know , Structure members are accessed using dot operator.
  2. 'date' structure is nested within Employee Structure.
  3. Members of the 'date' can be accessed using 'employee'
  4. emp1 & doj are two structure names (Variables)
How ?
Accessing Month Field :  emp1.doj.month   
Accessing day Field   :  emp1.doj.day  
Accessing year Field  :  emp1.doj.year

Way 2 : Declare Embedded structures
struct Employee
{
   char ename[20];
   int ssn;
   float salary;
   struct date
       {
       int date;
       int month;
       int year; 
       }doj;
}emp1;
Accessing Nested Members :
Accessing Month Field :  emp1.doj.month   
Accessing day Field   :  emp1.doj.day  
Accessing year Field  :  emp1.doj.year
Bookmark & Share


Tags / Keywords : | , ,

0 comments

Post a Comment

Your Feedback :This is Growing Site and Your Feedback is important for Us to improve the Site performance & Quality of Content.Feel Free to contact and Please Provide Name & Contact Email