Nesting of Structure in C
Defining one structure within another structure. is known as nesting of structure. We can do it as follows:
Program |
#include<stdio.h> struct DATE { int d; int m; int y; }; struct stud { int rno; char n[20]; struct DATE d1; }; int main() { struct stud s1; s1.rno=1; strcpy(s.n,”LadderPython”); s.d1.d=1; s.d1.m=3; s.d1.y=2010; printf(“\nRollno=%d”,s1.rno); printf(“\nName=%s”,s1.n); printf(“\nDate=%d/%d/%d”,s.d1.d,s.d1.m,s.d1.year); return(0); } |
Output |
Rollno=1 Name=LadderPython Date of Birth=1/3/2010 |
Description |
In the above program, variable d of structure dob has been declared inside the structure student. |
Program |
struct stud { int rno; char n[20]; struct DATE { int d; int m; int y; }d1; }; int main() { struct stud s1; s1.rno=1; strcpy(s.n,”LadderPython”); s.d1.d=1; s.d1.m=3; s.d1.y=2010; printf(“\nRollno=%d”,s1.rno); printf(“\nName=%s”,s1.n); printf(“\nDate=%d/%d/%d”,s.d1.d,s.d1.m,s.d1.year); return(0); } |
Output |
Rollno=1 Name=LadderPython Date of Birth=1/3/2010 |
Description |
In the above program, structure dob has been defined inside the structure student hence implementing nesting of structure. |
Best Sellers