C Nested Structure2 min read

Nested Structure in C is nothing but the structure within a structure. We can declare a structure inside a structure and have its own member function. With the help of nested structure, complex data types are created.

The struct declare inside another struct has its own struct variable to access the data members of its own. Following is the Syntax of nested structure.


Structure can be nested in two different ways:

1. By Embedding structure

Embedding method means to declare structure inside a structure. It is written in following way in C.

2. By separating structure

This is the second method where the structures are declared separately but the dependent structure is declared inside the main structure as one of its members. And it is written in the following way in C.


Accessing Nested structure in C

Accessing the members is done by (.) dot operator (already discussed). But in a nested structure, members of the nested structure (inner structure) is accessed through the outer structure followed by the (.) dot operator.

Consider the above Dept and Emp structure, if we want to access the Emp members, we need to access through Dept and is done in following way.

d1.e1.emp_name;
d1.e1.emp_id;
d1.e1.emp_add;

Note: You can extended the nesting of structure to any level.


C Program for Nested Structure

Output:

Nested structure in C

MORE

Keith Number Program in C

In this tutorial, we will write a Keith number in C. It is one of the most asked questions in an interview. Before that, you …
Read More

Keith Number Program in C++

In this tutorial, we will write a Keith number in C++. It is one of the most asked questions in an interview. Before that, you …
Read More

Singly linked list Program C++

In this tutorial, we will write a C++ program to implement singly linked list. In this program, we will create a separate function for each …
Read More

Fascinating Number Program in C

In this tutorial, we will write a C program to check whether the given number is a Fascinating number or not. You may go through …
Read More

Peterson Number in C

In this tutorial, we will learn about the Peterson number and check if the number is Peterson or not in C. What is Peterson Number? …
Read More

Peterson Number in C++

In this tutorial, we will learn about the Peterson number and write a C++ program to check if the given number is a Peterson number …
Read More