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

C Program to search an element in an array using Pointers

A separate function( search_function()) will be created where the array pointer will be declared and the searched element along with the size of an array …

C Program to find the sum of the digits of a number using recursion function

This C program calculates the sum of digits of a given number using recursion. Here’s a concise explanation: Function Definition: sumDigits(int n) This function calculates …

C program to find factorial of a numberĀ using Ternary operator with Recursion

Recursion refers to the function calling itself directly or in a cycle. Before we begin, you should have the knowledge of following in C Programming: …

C Program to Add Two Numbers Using Call by Reference

The program takes the two numbers from the user and passes the reference to the function where the sum is calculated. You may go through …

Find the output ab, cd, ef, g for the input a,b,c,d,e,f,g in Javascript and Python

In this tutorial, we will write a program to find a pairs of elements from an array such that for the input [a,b,c,d,e,f,g] we will …

String Pattern Programs in C

In this tutorial, we will write various C pattern programs for String. Before that, you may go through the following topics in C. for loop …