by Dinesh Thakur Category: Array Pointer and Union

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 ; I I 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





About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), 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.



Related Articles