Nested Structure in C with Examples

Learn via video course
FREE
View all courses
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
Topics Covered

Overview

Structure is a user-defined data type, which is used to store a group of items of different data types as a single data type. Each element of a structure is known as its member.

Suppose we have an entity 'Employee', and we need the name and employee id of each employee. Instead of storing all the names and employee ids of each employee in some array or vector, we can create a structure 'Employee' in the following manner:

Now let us see, what are nested structures. As the name suggests, nested structure refers to nesting one structure within another. In case of nested structure in C, the embedded structure will be the members of the outer structure. In this article we will study in depth about nested structures in C.

Prerequisite

Structures in C

Syntax

Basic Example of Nested structure in C

Ways in Which a Structure Can Be Nested

Let us now explore the various methods in which we can nest a structure within another in C. We will also see examples for all the methods discussed.

By Embedded Structure

As the name suggests, this method is used to embed one structure inside another, i.e defining one structure in the definition of another structure. Thus using this method, the nested structure will be declared inside the parent structure.

Embedded structure follows the below format:

Let us see how to achieve nested structure in C through embedded structure with the help of the following C program:

In the above example, the structure incomeInLPA has been embedded within structure employee. Thus parent structure 'employee' has three variables name, employeeid and inc. The further variable inc contains base_salary, CTC, and bonus. The structure incomeInLPA is defined inside the parent structure employee.

By Separate Structure

The two structures are created separately in this method. The Dependent structure is used inside the Main/Parent structure by taking a member of the dependent structure type in the definition of the parent structure.

This is done in the below format:

Let us see how to achieve nested structure in C through separate structures with the help of the following C program:

In the above example, both the structures have been declared separately. The structure IncomeInLPA has been defined first and it contains the variables base_salary, CTC and bonus. Then the parent structure employee has been defined and it has the variables name, employeeid, and the structure variable inc of the type structure incomeInLPA defined above.

Drawbacks of Nested Structure

The limitations of nested structures include:

  • Lack of Independent Existence: It's essential to understand that a structure like "Employee" cannot stand alone as a separate entity in the program. You cannot declare a structure variable of the "struct Employee" type outside the main structure.
  • Incompatibility with Multiple Data Structures: Nested structures are not suitable for use in various data structures because of the constraint on declaring structure variables within the primary structure. Therefore, the best practice is to employ a distinct structure that can be incorporated into multiple data structures.

Initializing Nested Structures

Nested Structures in C can be initialized at the time of declaration. There are two methods by which we can initialize nested structures:

Method 1 : The embed structure is initialized first using the structure variable of that structure, then the parent structure is initialized next using that already initialized member of the embed structure.

Let us understand this with the example below:

In the above program, employee is our parent structure and incomeInLPA is our nested/embed structure.

We initialize our nested structure first and create the structure variable inc. This structure variable is then used for initializing the parent structure employee variable emp.

Method 2 : Both the parent structure as well as nested structure are initialized together.

Let us see this with the help of the following example:

As seen in the above program, the incomeInLPA structure hasn't been initialized separately. "Mike", and 92 will be stored as name and employeid in the employee structure. {13, 27 ,13} will be stored as base-salary, ctc and bonus in incomeInLPA structure variable and these all three variables are a member of emp which is of type employee.

Passing Structure to Function

Pass the nested structure variable at once.

We can pass the individual members of the structure to the function separately as arguments. Here is the code for this:

Output:

Explanation: Here we access the inner member of the class Inner, using out.in.c, while we access the outer members using out.a and out.b, as discussed in the previous section. We individually pass these values to the function.

Pass the nested structure members as an argument into the function.

Alternatively, Nested structures can be passed to functions directly, by passing the structure as an argument to the function.

Example:

Here the structure variable 'out' of the outer structure is passed to the show function. Thus when show is called from main with 'out' as an argument, all the print statements of show are executed. Since out structure variable also contains the inner structure variable 'in', we can access members of the inner structure too as discussed in the previous section.

Output:

Accessing Nested Structure

We can access Nested Structure in C in the following two ways:

  • Using Normal variable.
  • Using Pointer variable.

Let us go over both of them one by one.

Using Normal Variable

Here the data members of the outer structure are accessed using a single dot, while data members of the inner structure are accessed using two dots. Here is a code for accessing nested structure using normal variable:

Output:

Here the outer structure has 3 variables a, b and in, while the further variable in contains c in it. We initialise a = 5, b = 10 and c = 15. If we want to access variables a and b, we can access them using a single dot as they are members of the outer structure. However for the inner structure variable c, we have to use two dots as: outerStructure.innerStructure.variable i.e, out.in.c, to access the variable c.

This is because for accessing the inner structure variable c, we first have to access 'in' variable of outer structure 'out'. Now to access any variable inside 'in' variable we'll have to specify which variable we want to use using another dot, thus we'll have to use out.in.c, to access the inner variable c.

Using Pointer Variable

We can also use pointer variables for accessing any data member of a structure. In the below example we use one pointer variable for the outer structure, while a normal variable for the inner structure.

Output:

Here due to usage of both normal (*in*) and pointer(*out_ptr*) variable, we use both dot(.) and arrow(->) for accessing the inner and outer structure data members respectively. As out_ptr is pointer variable, so to access the member of structure pointing by this pointer we have to use -> but to access 'c' we have to use dot further as in is normal variable and c could be accessed directly by in due to which we write out_ptr->in.c.

Example of Nested Structures in C

Now that we know how to declare, initialize and access nested structure in C, let us see all of these in a single example.

Let us suppose we have a structure for an intern working in a particular company. The company needs the following details from all of their interns - their name, intern-id, job-role, school details and college details.

To achieve this, let us keep a structure named intern. The data members of the intern are name, intern-id, and job-role. We keep two nested structures, school and college to store the school and college details for each intern. We create the school and college structure separately and use them inside our parent structure, which is intern.

Let us see the C code for this example and print all the details required by the company for a particular intern.

Output:

In the above program, intern is our parent structure and school, and college are our dependent structures.

We initialize our dependent structures first, and create the structure variables schl and clg, for structure school and college respectively. These two structure variables are then used for initializing the parent structure intern variable intrn.

While printing the details of an intern, a structure variable intrn is used and the data members of outer structure intern, are accessed using a single dot, eg - intrn.name, intrn.intern_id etc.

While printing the details of the nested structure variables, like school and college details of an intern, we'll have to follow the format outerStructure.innerStructure.variable. Since the outer structure variable is intrn, and nested structure variables are scl and clg, to access the school name and college name of an intern we have used intrn.schl.school_name, intrn.clg.college_name.

Conclusion

  • We learned that nested structures in C can be achieved by embedding one structure inside another.
  • We saw it can also be achieved by declaring the two structures separately and using the Dependant Structure inside the Parent Structure.
  • We learned about the two methods to initialize nested structure in C.
  • We saw how to access nested structure using Normal Variable and Pointer Variable.
  • We got to know that nested structure can be passed as an argument to function.