Nested Structures in C Programming

Nested Structures in C: In our previous post, we discussed Structures, and they are pretty good for grouping different data type members. However, there are some problems, such as listing more number of variables inside the structure and Code repetitiveness. To resolve these situations, we use Nested structures.

The Nested Structures in C example: Assume we are working with Employee, Student, and Person data. So, we want to declare three, i.e., Employee, Student, and Person. Each one may have different members (Ex: For Student: Roll Number, Class Number, Marks, etc.). But there may be some common members such as Age, door number, street name, and city. In general, adding door numbers, street names, and city members to all three structures means repetitive work!

Like Loops, Nested structures in C programming are the Structures inside the other. Using this, we can create the first one called to address and add door number, street name, and city members under it. Now, we can call this address from the Employee, Student, and Persons. We can declare the nested structure in multiple ways:

First Approach for Nested Structures in C Programming

First, declare the address structure and then create the address variable inside the Employee. Please refer to Introduction and Loops articles.

Declaring the address Struct

struct address
{
  int Door_Number;

  char Street_Name[20];

  char City[20];

  int Postcode;

} ;

Here, declaring the Employee Struct

struct Employee
{
  int age;

  char Name[50];

  char Department[20];

  struct address add; // creating the address variable = add

  float salary;

} ;

Second Approach

I am declaring the address with its members directly inside the Employee struct.

struct Employee
{
  int age;
  char Name[50];
  char Department[20];
  float salary;

  struct address // Declaring the address
   {
     int Door_Number;
     char Street_Name[20];
     char City[20];
     int Postcode;
   } add; // creating the address variable = add
};

Program to Display Nested Structures in C Programming

In this Program, We are going to declare Nested structures (one inside the other). Then we are going to display them in two ways:

  1. Create Structure Variable and assign the corresponding values to them (Normal Variable)
  2. Create Struct Pointer Variable and assign the corresponding values to them (Pointer Variable)

In this Nested Structures example, we declared the address struct with Door_Number (integer data type), and City (character array of 20 bytes or, say, string)

Next, declare the Employee with Age, Name, Salary, and struc variable of the address and add members with appropriate Data Types. From the below C Programming code, you can observe that we created the pointer variable (i.e., *emp3) at the time of struct declaration only.

#include<stdio.h>
#include<string.h>

struct address
{
  int Door_Number;
  char City[20];
};

struct Employee
{
  int Age;
  char Name[50];
  struct address add;
  float Salary;  
}*emp3;

int main() 
{
  struct Employee emp1 = { 25, "Tutorial", 222, "Liverpool", 25000.50 };
  struct Employee emp2;
    
  emp2.Age = 28;
  strcpy(emp2.Name, "Gateway" );
  
  emp2.add.Door_Number = 145;
  strcpy(emp2.add.City, "Manchester" );
  
  emp2.Salary = 45000.00; 
  
  emp3 = &emp2;
  
  printf("\n Details of the Employee 1 \n " );
  printf(" Employee Age = %d \n ", emp1.Age );
  printf(" Employee Name = %s \n ", emp1.Name );
  printf(" Employee Door Number = %d \n ", emp1.add.Door_Number );
  printf(" Employee City = %s \n ", emp1.add.City );
  printf(" Employee Salary = %.2f \n\n ", emp1.Salary );

  printf(" Details of the Employee 2 \n " );
  printf(" Employee Age = %d \n ", emp3->Age );
  printf(" Employee Name = %s \n ", emp3->Name );
  printf(" Employee Door Number = %d \n ", emp3->add.Door_Number );
  printf(" Employee City = %s \n ", emp3->add.City );
  printf(" Employee Salary = %.2f \n ", emp3->Salary );
  
  return 0;
}
Nested Structures in C Programming

Within the main() function, we created the Employee struct variable emp1 and then assigned the values to them.

struct Employee emp1 = { 25, "Tutorial", 222, "Liverpool",25000.50 };

It means the values for emp1 are as follows.

Age = 25

Name = Tutorial

Door Number = 222 //address variable

City = Liverpool //address variable

Salary = 25000.50

Created one more variable emp2. This time, we used the member’s operator or dot operator (.) to assign the values as per the data type.

struct Employee emp2;

emp2.Age = 28;

We have to use the strcpy() built-in function to assign values for the strings.

strcpy(emp2.Name, "Gateway");

emp2.Salary = 45000.00;

The door Number and City are the members of the address structure. So, to access them, we have to use the below format:

Variable. Nst_Variable. Member

Here, the Variable is emp2, and the Nst_Variable added so,

emp2.add.Door_Number = 145;

strcpy(emp2.add.City, "Manchester");

Assigning the address of the emp2 to variable emp3 using the below statement

emp3 = &emp2;

The Printf statement is to output each member present in the structure variables.

Points to Remember in C Nested Structures

  1. To print the structure variable, we have to use the dot operator (.)
  2. To print the nested structure variable, we have to use two dot operators such as (Variable. Nst_Variable. Member)
  3. printing the pointer variable, we have to use an arrow operator instead of the dot operator. (Variable -> Member)
  4. To print the nested variable using pointers, we have to use a combination of the arrow operator and dot operator. (Variable -> Nst_Variable.Member)