Structure within a Structure
- The nested structure in C is nothing other than the structure inside a structure. A structure can be declared within another structure as we declare the structural members within a structure.
- Structure variables may be a normal structure variable or a pointer variable for accessing data.
- This program describes how to use the structure inside the C structure using a normal variable. “the structure of student_college_detail is declared inside the “student_detail” structure of this program. The two structural variables are standard structural variables.
- Please note that the members of the “student_college_detail” structure are accessed by a two-point operator (.) and the members of the “student_detail” structure are accessed by a single point operator (.).
Example program for Structure within a Structure:
#include <stdio.h>
#include <string.h>
struct student_college_detail
{
int college_id;
char college_name[50];
};
struct student_detail
{
int id;
char name[20];
float percentage;
// structure within structure
struct student_college_detailclg_data;
}stu_data;
int main()
{
struct student_detailstu_data = {1, "Raju",
90.5, 71145,"GITAM University"};
printf(" Id is: %d \n", stu_data.id);
printf(" Name is: %s \n", stu_data.name);
printf(" Percentage is: %f \n\n",
stu_data.percentage);
printf(" College Id is: %d \n",
stu_data.clg_data.college_id);
printf(" College Name is: %s \n",
stu_data.clg_data.college_name);
return 0;
}
Output:

Example for structure within structure or nested structure:
#include<stdio.h>
struct Address
{
char HouseNo[25];
char City[25];
char PinCode[25];
};
struct Employee
{
int Id;
char Name[25];
float Salary;
struct Address Add;
};
void main()
{
int i;
struct Employee E;
printf("\n\tEnter Employee Id : ");
scanf("%d",&E.Id);
printf("\n\tEnter Employee Name : ");
scanf("%s",&E.Name);
printf("\n\tEnter Employee Salary : ");
scanf("%f",&E.Salary);
printf("\n\tEnter Employee House No : ");
scanf("%s",&E.Add.HouseNo);
printf("\n\tEnter Employee City : ");
scanf("%s",&E.Add.City);
printf("\n\tEnter Employee House No : ");
scanf("%s",&E.Add.PinCode);
printf("\nDetails of Employees");
printf("\n\tEmployee Id : %d",E.Id);
printf("\n\tEmployee Name : %s",E.Name);
printf("\n\tEmployee Salary : %f",E.Salary);
printf("\n\tEmployeeHouseNo : %s",E.Add.HouseNo);
printf("\n\tEmployee City : %s",E.Add.City);
printf("\n\tEmployeeHouseNo : %s",E.Add.PinCode);
}
Output:
