Nesting of Structure in C
Defining one structure within another structure. is known as nesting of structure. We can do it as follows:
Program to demonstrate nesting of structure. |
#include<string.h> #include<stdio.h> struct dob { int dd; int mm; int yy; }; struct std { int roll; char sname[20]; struct dob d1; }; int main() { struct std s1; s1.roll=131; strcpy(s1.sname,”Aman”); s1.d1.dd=23; s1.d1.mm=3; s1.d1.yy=2022; printf(“\nroll=%d”,s1.rollo); printf(“\nsname=%s”,s1.name); printf(“\nDate=%d/%d/%d”,s1.d1.dd,s.d1.mm,s1.d1.yy); return(0); } |
Output |
Rollno=131 Name=Lovejot Date of Birth=11/7/2007 |
Description |
In the above program, variable d of structure dob has been declared inside the structure student. |
Popular Books of Computer Science