What is the Nested Structure in C Language with Free Notes

Nested Structure

A Structure can appear with another Structure. This process is known as Nesting Structure. There is not limit to nesting of Structures. For Instance, the date of birth (DOB) is a Structure within the Structure of a student as shown in fig.

The Nested Structure shown in fig. can be declared as,

struct date 
{
   int dd;
   int mm;
   int yy;
};
struct student
{
   char name[20]
   int roll;
   struct date dob;
   int marks;
 };

OR

struct student
{
   char name[20];
       int roll;
       struct date;
          {
           int dd;
           int mm;
           int yy;
          }dob;
             int marks;
 };

Accessing Members of Nested

  • It may be noted that the member of a nested Structure is reference from the outermost to innermost with the help of dot operators.
  • Let us declare a variable s1 of type student.
Free Programming E-Books
struct student s1;
  • All Members of nested structure can be accessed as
s1.dob.dd = 20
s1.dob.mm = 10
s1.dob.yy = 99
  • It means that store value 20 in dd, 10 in mm, 99 in yy of a structure dob which itself is a member of structure variable s1.

Example: Write a program to demonstrate nested structure of above type,

#include<stdio.h>
#include<conio.h>
void main()
{
  struct date 
{
   int dd;
   int mm;
   int yy;
};
struct student
{
   char name[20]
   int roll;
   struct date dob;
   int marks;
 } s1;
printf("\n Enter data of a student");
printf("\n Enter name");
gets(s1.name);
printf("\n Enter roll no.");
scanf("%d",&s1.roll);
printf("\n Enter data of birth dd mm yy:");
scanf("%d%d%d", &s1.dob.dd, &s1.dob.mm, &s1.dob.yy);
printf("\n Enter marks");
scanf("%d", &s1.marks);
printf("\n Record of student is...");
printf("%s\t%d\t%d%d%d\t%d", s1.name, s1.roll, s1.dob.dd, s1.dob.mm, s1.dob.yy, s1.marks);
getch();
}

Outputat

Enter data of Student 
Enter name: Pawanpreet Singh 
Enter roll no. : 1251
Enter date of birth dd mm yy: 16 11 02
Enter marks: 90

Record of Student is 
Pawanpreet Singh 1251 16 11 2002 90

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top