A structure can be placed within another structure, i.e., a structure may contain another structure as its member. A structure that contains another structure as its member is called a nested structure.
NESTED
STRUCTURES
A
structure can be placed within another structure, i.e., a structure may contain
another structure as its member. A structure that contains another structure as
its member is called a nested structure.
Let
us now see how we declare nested structures or structures that contain
structures. Although it is possible to declare a nested structure with one declaration,
it is not recommended. The easier and clearer way is to declare the structures
separately and then group them in a high- level structure. When you do this,
take care to check that nesting must be done from inside out (from lowest level
to the most inclusive level), i.e., to say, declare the innermost structure,
then the next level structure, working towards the outer (most inclusive)
structure.
typedef struct
{
char first name [20];
char mid_name [20];
char last name [20];
}NAME;
typedef struct
{
int dd;
int mm;
int yy:
}DATE;
typedef struct student
{
int r_no;
NAME name;
char course [20];
DATE DOB;
float fees;
};
In
this example, we see that the structure student contains two other
structures-NAME and DATE. Both these structures have their own fields. The
structure NAME has three fields: first_name,
mid_name, and last_name. The structure DATE also has three fields: dd, mm,
and yy, which specify the day, month, and year of the date. To assign values to
the structure fields, we will write
student stud1;
{
studl.name.first_name
="Janak";
studl.name.mid_name =
"Raj";
studl.name.last_name =
"Thareja";
stud1.course = "BCA";
studl.DOB. dd = 15;
stud1.DOB.mm = 09;
studl.DOB.yy = 1990;
studl.fees = 45000;
In
case of nested structures we use the dot operator in conjunction with the
structure variables to access the members of the innermost as well as the
outermost structures. The use of nested structures is illustrated in the
following program:
5.
Write a program to read and display information of a student using a structure
within a structure.
#include <stdio.h>
#include <conio.h>
int main()
{
struct DOB
{
int day;
int month;
int year;
};
struct student
{
int roll_no;
char name [100];
float fees;
struct DOB date;
};
struct student stud1;
clrscr();
printf("\n Enter the roll
number: ");
scanf("%d", &stud1.roll_no);
printf("\n Enter the name:
");
scanf("%s", stud1.name);
printf("\n Enter the fees:
");
scanf("%f", &stud1.fees);
printf("\n Enter the DOB:
");
scanf("%d %d %d",
&stud1.date.day, &stud1.date.month, &stud1.date. year);
printf("\n *** STUDENT'S
DETAILS ***");
printf("\n ROLL No. =
%d", stud1.roll_no);
printf("\n NAME %%s",
stud1.name);
printf("\n FEES = %f",
stud1. fees);
printf("\n DOB %d %d %d",
stud1. date.day, stud1.date.month, stud1.date. year);
getch();
return 0;
}
Output
Enter the roll number: 01
Enter the name: Rahul
Enter the fees: 45000
Enter the DOB: 25 09 1991
********STUDENT'S DETAILS ****
ROLL NO. = 01
NAME = Rahul
FEES = 45000.00
DOB = 25-09-1991
Programming in C: Unit IV: Structures and Union : Tag: : with Example C Programs - Nested Structures
Programming in C
CS3251 2nd Semester CSE Dept 2021 | Regulation | 2nd Semester CSE Dept 2021 Regulation