Nested structures in C language
In C language, a structure can contain another structure as one of its members. This is called a nested structure.
Example:
#include <stdio.h>
#include <string.h>
struct date {
int day;
int month;
int year;
};
struct student {
char name[50];
int roll_number;
struct date birthdate;
};
int main() {
struct student s1;
strcpy(s1.name, "John");
s1.roll_number = 1234;
s1.birthdate.day = 15;
s1.birthdate.month = 9;
s1.birthdate.year = 1990;
printf("Name: %s\n", s1.name);
printf("Roll Number: %d\n", s1.roll_number);
printf("Birthdate: %d/%d/%d\n", s1.birthdate.day, s1.birthdate.month, s1.birthdate.year);
return 0;
}
Output:
Name: John
Roll Number: 1234
Birthdate: 15/9/1990
In this example, the struct student has a member birthdate of type struct date. This means that each student has a birthdate which is a structure with three members: day, month, and year. The program creates a struct student variable s1 and assigns values to its members using dot notation. It then prints out the values of s1‘s members, including the nested birthdate structure.