Structures within a structure mean nesting of structures. As in the case of looping, there is also the permission for the nesting of the structures. Consider an example to store the information about the salary of employees which is as:
struct salary
{
char name[20];
cahr department[25];
int basic_salary;
int health_allowence;
int house_rent_allowence;
int city_allowence;
}employee;
This structure defines name, department, basic salary and three types of the allowances. We can group all the items related to allowance together and declare them under a substructure as:
struct salary
{
char name[20];
cahr department[25];
int basic_salary;
struct
{
int health;
int house_rent;
int city;
}allowence;
}employee;
OR
struct facility
{
int health;
int house_rent;
int city;
};
struct salary
{
char name[20];
cahr department[25];
int basic_salary;
struct facility allowance;
}employee;
How to access the member variables of the nesting structure?
To access the member variables of the nesting structure we have to use two dot (.) operators.
The syntax to access the member variable of the nesting structure will be:
Structure_name.sub-structure_name.member_variable;
In the above example, the member variables of the nesting structure can be referred as:
Employee.allowence.health;
Employee.allowence.house_rent;
Employee.allowence.city;
No Comments