The Power of Nested Structures: A Comprehensive Guide
Nested Structures
Its possible to write one structure inside another structure. Such structures are called ‘nested structures‘. This can be achieved by declaring one structure variable inside another structure as an element.
struct employee
{
int id;
char name[20];
};
Suppose, we want to use this structure to store employee date of birth also, it can be represented as another structure
struct dob
{
int dd;
int mm;
int yy;
};
To include this structure into employee structure, we can declare dob structure variable as an element inside employee structure as
struct employee
{
int id;
cha name[20];
struct dob d;
};
struct employee e;
In this way, dob structure is nested inside the employee structure. Now, all the elements of dob structure are also available to employee structure. Hence to refer to all the elements of the employee structure, we can write:
e.id; /* employee structure element */
e.name; /* employee structure elemet */
e.d.dd; /* employee's dob structure element */
e.d.mm; /* employee's dob structure element */
e.dd.yy; /* employee's dob structure element */