Posted in All Posts, Semester 2

Structures within Structures

We now know what are Arrays within Structures. Now we will learn what are Structures within structures. Structures within Structures basically mean nesting of structures. Nesting of structures is permitted in C.
A structure can be declared in another structure like we declare structure members inside a structure.
Now, let’s see how to declare a structure within structure.

Declaring structure within Structure

Below is an example to show the same-

Here, struct salary is a nested structure within struct emp.
Please note– Variables of a nested structure can be declared only with the structure declaration.

Accessing a nested structure

To access a member variable of a nested structure you need to give a reference to the parent structure (emp), then to the child structure (salary) and finally to the member variable.
For example- To access the basic salary of the first employee, the valid code will be like this emp[0].sal.basic

Defining structures using tag names

We can also use structures to define inner structures.
For example,

Structures within Structures Example

Checkout the example below-

We hope that you found this article to be helpful.
Please share your thoughts in the Comments Section.

If you liked this article, then please like our Facebook page.

See Structures & Functions.

Suggested Reading

Comment your thoughts