NESTED STRUCTURE (STRUCTURE AS A MEMBER TO STRUCTURE)
In C programming, a nested structure refers to a structure definition that contains another structure as one of its members. This allows you to create a hierarchical relationship between structures, where the outer structure encompasses the inner structure.
Syntax:
#include <stdio.h>
int main() {
// Define an inner structure
struct Date {
int day;
int month;
int year;
};
// Define an outer structure that contains the inner structure
struct Student {
char name[50];
int age;
struct Date dob; // Nested structure
};
// Declare a variable of the outer structure type
struct Student s;
-----------
----------
}
Example:
WAP to create a structure named date that has day,month and year as its members. Include this structure as a member in another structure named Student which has name,age and marks as other member . Use this structure to read and display employees name ,age, date of birth and marks.
#include<stdio.h>
struct date
{
int day;
int month;
int year;
};
struct Student
{
char name[10];
int age;
struct date dob;
float marks;
}s;
int main()
{
printf("enter the information of student\n");
printf("Enter name\n");
scanf("%s",s.name);
printf("Enter age\n");
scanf("%d",&s.age);
printf("\n Day of birthday:\t");
scanf("%d",&s.dob.day);
printf("\n Month of birthday:\t");
scanf("%d",&s.dob.month);
printf("\n Year of birthday:\t");
scanf("%d",&s.dob.year);
printf("Enter marks\n");
scanf("%f",&s.marks);
printf("\n THE STUDENT INFORMATION IS \n");
printf("NAME \t\t AGE \t\t DAY\t\t MONTH\t\t YEAR\t\t MARKS\n");
printf("%s\t\t%d\t\t%d\t\t%d\t\t%d\t\t%.2f",s.name,s.age,s.dob.day,s.dob.month,s.dob.year,s.marks);
return 0;
}