Nested Structure in C
Nested Structure
A nested structure is a structure that contains another structure as one of its members.
This means that a nested structure can be defined within another structure.
Example of a nested structure:
struct date {
int day;
int month;
int year;
};
struct person {
char name[50];
int age;
struct date birthdate;
};
Explanation
- The
person
structure contains a nesteddate
structure as one of its members. - The
date
structure has three members:day
,month
, andyear
. - The
person
structure has three members:name
,age
, andbirthdate
, wherebirthdate
is of typedate
.
Acessing members of the nested structure
To access the members of the nested structure, you can use the dot
operator twice, once for the outer structure and once for the inner structure.
For example:
john.birthdate.day = 1;
john.birthdate.month = 1;
john.birthdate.year = 2000;
Example of nested structure
#include <stdio.h>
struct date
{
int day;
int month;
int year;
};
struct person
{
char name[50];
int age;
struct date birthdate;
};
int main()
{
struct person john;
// assign values to the members of the john structure
strcpy(john.name, "John");
john.age = 25;
john.birthdate.day = 1;
john.birthdate.month = 1;
john.birthdate.year = 2000;
// access the members of the john structure and print them out
printf("Name: %s\n", john.name);
printf("Age: %d\n", john.age);
printf("Birthdate: %d/%d/%d\n", john.birthdate.day, john.birthdate.month,
john.birthdate.year);
return 0;
}
Output:
Explanation
- We define a nested structure called
date
within theperson
structure. - We then declare a person variable called
john
and assign values to its members, including the birthdate member which is a nested structure. - We then use the
dot
operator twice to access theday
,month
, andyear
members of the birthdate structure, and print them out usingprintf()
.
Output:
Name: John
Age: 25
Birthdate: 1/1/2000
When to use nested strutures
We use nested structures in C when we need to represent more complex data structures that have multiple levels of abstraction.
A nested structure is particularly useful when one or more members of a structure are themselves structures.
For example:
Consider a program that manages a company's employee database. Each employee may have a personal information structure, an employment information structure, and a salary structure, each with its own set of members. We could use nested structures to represent each employee's record, with the employee structure containing the personal, employment, and salary structures as members.
Nested structure for representing an employee record:
struct personal_info {
char name[50];
char address[100];
char phone[20];
};
struct employment_info {
char job_title[50];
char department[50];
char hire_date[20];
};
struct salary_info {
double base_salary;
double bonus;
double tax_rate;
};
struct employee {
int employee_id;
struct personal_info personal;
struct employment_info employment;
struct salary_info salary;
};
Explanation
- We have defined four structures:
personal_info
,employment_info
,salary_info
, andemployee
. - The
employee
structure contains thepersonal_info
,employment_info
, andsalary_info
structures as members.