Nested Structure:
When members of a structure are defined as structure type, these are called nested structures. For example:
struct info
{
char s_name[15];
char f_name[15] ;
char city[15] ;
int age;
};
struct p_data
{
info s1;
info s2;
float x;
};
p_data rec;
In the above example, info structure is defined with four members. The structure p_data is defined after the info and it contains three members; two are of structure type and one of float type. The members s1 and s2 within the structure are the nested structures. The structure variable rec is of nested structure type. It is like a structure of array. The structures can he nested to any degree.
Initialization of Nested Structures:
The structure variable that contains members of structure type is initialized as the structure of array is initialized. For example, the above structure variable “rec” may he initialized as:
p_data rec = {{“John”, “William”, London”, 20) {“Joseph”, “Anthony”, “Canada”, 30}, 6.9};
Accessing Members of Nested Structures:
The members of nested structures arc accessed in the same way as the members of simple structures arc accessed. However, to access members of nested structures, more than one dot operators are used.
For example:
rec.s1.age = 22;
In the above statement rec is the name of the structure variable, s1 is member of structure p_data and age is the member of structure info.
comment closed