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

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 …
Read More

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 …
Read More

Java Program to Find pair of Integers in Array whose sum is given Number

In this tutorial, we will write a program to find a pair of elements from an array whose sum equals a given number in java …
Read More

Program to Print Diamond Alphabet Patterns in C

In this tutorial, we will learn to write a C program to print Diamond patterns using alphabets/characters. However, in this tutorial, we will create a …
Read More

Half Diamond Pattern in C using Alphabets

In this tutorial, we will learn and code the half diamond alphabet patterns in C programming language. However, in this tutorial, we will create a …
Read More

Half Pyramid of Alphabets in C

In this tutorial, we will learn and code alphabet patterns in C programming language specifically the Half pyramid of alphabets in C programming. However, in …
Read More