Like other user defined data types, a structure can also include another structure, as its member, in its definition. A structure defined within another structure is known as a nested structure.
To understand the concept of nested structures, consider this example.
Example: A code segment to demonstrate nested structures
struct date // inner structure definition { int day; int month; int year; }; struct employee // outer structure { int emp_no ; char name [10]; date dob ; //inner structure };
In this example, the structure employee contains a structure variable of date type struct1,lre as one of its member. Now, the members of employee can be initialized using either of these statements.
employee emp1={101,"Smith",{22,4,1975}}; or employee emp1= {101, “Smith" ,22,4,1975} ;
The members of a nested structure can be accessed using these statements.
cout<<emp1.dob.month; //display month of emp1 emp1.dob.day=26; //assign value to day //member of emp1