Nested Structures in C Programming

Nested Structures in C: In our previous post, we discussed Structures, and they are pretty good to group different data type members. However, there are some problems, such as listing more number of variables inside the structure and Code repetitiveness.

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

To resolve these situations, C Programming allows us to use Nested structures. Like Loops, Nested structures in C are the Structures inside the Structure. Using this, we can create one structure called address and add door number, street name, city members under it. Now, we can call this address structure from the Employee, Student, and Person structures. We can declare the C nested structure in multiple ways:

First Approach for Nested Structures in C Programming

First, declaring the address structure, and then creating the address variable inside the Employee structure. Please refer to Structures and Loops articles.

Declaring the address Structure

struct address
{
  int Door_Number;

  char Street_Name[20];

  char City[20];

  int Postcode;

} ;

Here, declaring the Employee Structure

struct Employee
{
  int age;

  char Name[50];

  char Department[20];

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

  float salary;

} ;

Second Approach for Nested Structures in C Programming

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

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

  struct address // Declaring the address structure
   {
     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 C nested structures Program, We are going to declare Nested structures (structure inside the structure). Then we are going to display them in two ways:

  1. Creating Structure Variable and assign the corresponding values to them (Normal Variable)
  2. Creating Structure Pointer Variable and assign the corresponding values to them (Pointer Variable)
/* Program to display Nested Structures in C Programming */
#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

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

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

Declared the Employee structure with Age, Name, Salary, and structure variable of the address add members with appropriate Data Types.

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

} *emp3;

From the above C Programming code, you can observe that we created the pointer variable (i.e., *emp3) at the time of structure declaration only.

Within the main() function, we created the Employee structure 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 structure variable

City = Liverpool //address structure variable

Salary = 25000.50

Created one more Employee structure 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;

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

strcpy(emp2.Name, "Gateway");

emp2.Salary = 45000.00;

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

Structure Variable. Nested Structure Variable. Structure Member

Here, Structure Variable is emp2, and C Nested Structure Variable added so,

emp2.add.Door_Number = 145;

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

Assigning the address of the emp2 to structure 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 C nested structure variable, we have to use two dot operators such as (Structure Variable. Nested Structure Variable. Structure Member)
  3. printing the structure pointer variable, we have to use an arrow operator instead of the dot operator. (Structure Variable -> Structure Member)
  4. To print the nested structure variable using pointers, we have to use a combination of the arrow operator and dot operator. (Structure Variable -> Nested Structure Variable. Structure Member)