Structure within Structure: A nested structure in C programming language is concept of defining structure within another structure.
It gives a flexibility to create a nested relationaship between different data elements.
Related data items can be organized in a more logical and structured manner by using netsed structure.
Every structure in C language can have its own members or attributes, including other structure as members.
Nested structure used in C programming allows us to represents relationship between complex data structure.
Structure within structure in C Programming.
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.
Other Topics:
Variables and Identifiers
Relational Operators
if-else statements
Switch case
While Loop
Infinite while Loops
C FOR Loop
Infinite for Loops
Continue in Loops
One Dimensional Array
Two Dimensional Arrays
Read and Display 2D Arrays
Types of functions
Passing Array To Functions
Nesting of Function
Array vs Structure
Array of Structure
Structures and Functions
Structures Within Structures
Use Of Pointers In C
File Handling In C
Loops FAQ
Arrays FAQ
count vowels in a file
Function FAQ
Conditional Statements Assignments
For Loops Assignments
Arrays Assignments
Function Assignments
Structure Assignments
Pointers Assignments
Files Assignments
Storage classes Assignments
Binary Files
count words,lines in a file
Copy files
Update File
Continue in Loops
break in Loops
Difference Between While and Do while
difference while do..while & for
malloc
calloc
Storage Classes
Operators MCQ
Conditional Statements MCQ
Loops MCQ
Arrays MCQ
Function MCQ
Structure MCQ
Pointers MCQ
Files MCQ
Storage classes MCQ