Table of Contents
show
Nested structure is one when a structure is contained inside another one. Two ways to specify nested structure,
- By specifying separate structure
- By specifying embedded structure
Specifying separate structure,
Syntax
struct struct_name1
{
statement (s);
…………………….
……………………
};
struct struct_name2
{
statement (s);
………………………..
………………………….
struct struct_name1 obj;
};
Example
struct school
{
char school_Name[50];
char Town[20];
int school_id;
};
struct student
{
int stu_id;
char stud_name;
int marks;
float avg;
struct school obj;
}s;
Specifying embedded structure
Syntax
struct struct_name1
{
statement (s);
…………………….
……………………
struct struct_name2
{
statement (s);
………………………..
………………………….
}o1;
}o2;
Example
struct school
{
char school_Name[50];
char Town[20];
int school_id;
struct student
{
int stu_id;
char stud_name;
int marks;
float avg;
}s;
}sch;
Accessing the members within the structure
Syntax
Outer_Structure_variable.Inner_Structure_variable.member
Example
sch.s.stu_id; // accessing the member of inner structure
sch.Town; // accessing the member of outer structure
Example: Â Method 1: Separate structure
#include<stdio.h>
struct school
{
char school_Name[50];
char Town[20];
int school_id;
};
struct student
{
int stu_id;
char stud_name[10];
int mark1;
int mark2;
float avg;
struct school obj;
};
int main()
{
struct student s;
// get the details of the members
printf("Enter the student id:");
scanf("%d",&s.stu_id);
printf("Enter the student name:");
scanf("%s",s.stud_name);
printf("Enter the mark1:");
scanf("%d",&s.mark1);
printf("Enter the mark2:");
scanf("%d",&s.mark2);
s.avg = (s.mark1 + s.mark2) / 2;
printf("Enter the school name:");
scanf("%s",s.obj.school_Name);
printf("Enter the town:");
scanf("%s",s.obj.Town);
printf("Enter the school id:");
scanf("%d",&s.obj.school_id);
// printing details
printf("****Details are:****\n");
printf("Stud_id\t stud_name \t mark1 \t mark2 \t average \t schoolName \t Town \t School_Id\n");
printf("%d \t %s \t %d \t %d \t %.2f \t %s \t %s \t %d \n",s.stu_id,
s.stud_name, s.mark1,s.mark2,s.avg,s.obj.school_Name,s.obj.Town,s.obj.school_id);
return 0;
}
Output
C:\TDM-GCC-64>gcc 4_Nested_way1.c -o 4Nw1
C:\TDM-GCC-64>4Nw1
Enter the student id:101
Enter the student name:abc
Enter the mark1:90
Enter the mark2:90
Enter the school name:Lions
Enter the town:Chennai
Enter the school id:234
****Details are:****
Stud_id stud_name mark1 mark2 average schoolName Town School_Id
101 abc 90 90 90.00 Lions Chennai 234
Example:Â Method 2: Embedded structure
#include<stdio.h>
struct employee
{
char name[25];
int emp_no;
struct joint_date
{
int dd,mm,yyyy;
}jd;
};
int main()
{
struct employee e;
printf("Enter name:");
scanf("%s",e.name);
printf("Enter employee number;");
scanf("%d",&e.emp_no);
printf("Enter joining date:");
scanf("%d%d%d",&e.jd.dd,&e.jd.mm,&e.jd.yyyy);
printf("****Details are:****\n");
printf("Employee name:%s\n",e.name);
printf("Employee name:%d\n",e.emp_no);
printf("Joining date of Employee:%d - %d - %d",e.jd.dd,e.jd.mm,e.jd.yyyy);
return 0;
}
Output
C:\TDM-GCC-64>gcc 4_Nested_way2.c -o 4NW2
C:\TDM-GCC-64>4NW2
Enter name:ABC
Enter employee number;101
Enter joining date:23
4
2002
****Details are:****
Employee name:ABC
Employee name:101
Joining date of Employee:23 - 4 - 2002
Views: 0