Structure within structure is aslso called nested structure in C language.

From the given fig. let us understand structure within structure with its syntax and example in C programming language.
let understand the structure syntax and structure example from the given fig.
Syntax: struct structure_name1
{
  structure member1;
  structure member2;
 .
 .
};
struct structure_name2
{
  structure member1;
  structure member2;
 struct structure_name1 var_name;
};
from the above syntax we can easily undesrstand that structure_name2 has the structure as member.
struct structure_name1 var_name is the member of structure.
Structure member can be accessed using the membership . Operator
To access the structure member we can use the following ways.
first of all let we creates the structure_name2 variable and then can access the structure members, following is the example for the same.
create the structure variable.
struct structure_name2 var1;
We can access the structure member2 of structure_name1 by using the following statement:
var1.var_name.member2;


Structure within Structure Example in C language.

The example shows how to declare,create and access individual elements of nested structure in c programming Language.

struct Department
{
 int deptno;
 char dname[25];
 char loc[20];
};
struct Employee
{
 int empno;
 char ename[15];
 char job[15];
 float salary;
 struct Department dept;
};
int main()
{
 // Structure variable
 struct Employee emp;
 //Assign values to nested structure members
 emp.dept.deptno =20;
 strcpy(emp.dept.dname, "Marketing");
 emp.dept.loc="PUNE";
 emp.empno=101;
 strcpy(emp.ename,"John");
 strcpy(emp.job,"Salesman");
 emp.salary = 40000;
 // Printing the details
 printf("Department Number : %d\n", emp.dept.deptno);
 printf("Department Name : %s\n", emp.dept.dname);
 printf("Department Location: %d\n", emp.dept.loc);
 printf("Employee id : %d\n", emp.empno);
 printf("Employee Name : %s\n", emp.ename);
 printf("Employee Job :%s\n",emp.job);
 printf(" Employee salary=%.2f",emp.salary);
}


Output:
 Department Number :20
 Department Name : Marketing
 Department Location:PUNE
 Employee id :101
 Employee Name :John
 Employee Job :Salesman
 Employee salary:40000

Program Explanation:
in the given example above we have defined two structures Departments and employee. Inside employee structure the struct Department dept is the nested structure member variable.
In the main() program we have create a variable of nested structure by the use of following statement . struct employee emp; where emp is the variable of nested structure.
After that all the nested structure member is initilized to some values using the membership operator (.).

 emp.dept.deptno =20;
 strcpy(emp.dept.dname, "Marketing");
 emp.dept.loc="PUNE";
 emp.empno=101;
 strcpy(emp.ename,"John");
 strcpy(emp.job,"Salesman");
 emp.salary = 40000;

Finally the nested structure mebers values are displayed with the help of given following statements.

 printf("Department Number : %d\n", emp.dept.deptno);
 printf("Department Name : %s\n", emp.dept.dname);
 printf("Department Location: %d\n", emp.dept.loc);
 printf("Employee id : %d\n", emp.empno);
 printf("Employee Name : %s\n", emp.ename);
 printf("Employee Job :%s\n",emp.job);
 printf(" Employee salary=%.2f",emp.salary);

Previous Topic:-->> Array Within Structure || Next topic:-->>Structure and Function.