Open In App

Nested Structure in C with Examples

Last Updated : 21 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

A nested structure in C is a structure within a structure. One structure can be declared inside another structure in the same way structure members are declared inside a structure.

Example:

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

// Declaration of Outer structure
struct College  {
	char college_name[20];
	int ranking;

	// Declaration of Inner structure
	struct Student {
		int student_id;
		char name[20];
		int roll_no;

		// Inner structure variable
	} student1;
};

int main() {
	struct College c1 = {"GeeksforGeeks", 7,
		{111, "Paul", 278}
	};

	printf("College name : %s\n",
	       c1.college_name);
	printf("Ranking : %d\n",
	       c1.ranking);
	printf("Student id : %d\n",
	       c1.student1.student_id);
	printf("Student name : %s\n",
	       c1.student1.name);
	printf("Roll no : %d\n",
	       c1.student1.roll_no);
	return 0;
}

Output
College name : GeeksforGeeks
Ranking : 7
Student id : 111
Student name : Paul
Roll no : 278

Creating Nested Strucutres in C

A nested structure allows the creation of complex data types according to the requirements of the program.

Syntax:

C
struct name_1 {
    member1;
    member2;
    .
    .
    membern;
    struct name_2 {
        member_1;
        member_2;
        .
        .
        member_n;
    }, var1
} var2;

The members of a nested structure can be accessed using the following syntax:

C
Outer_Structure.Nested_Structure.data member

Example:

  • Consider there are two structures Employee (depended structure) and another structure called Organisation(Outer structure).
  • The structure Organisation has the data members like organisation_name,organisation_number.
  • The Employee structure is nested inside the structure Organisation and it has the data members like employee_id, name, salary.

For accessing the members of Organisation and Employee following syntax will be used:

C
org.emp.employee_id;
org.emp.name;
org.emp.salary;
org.organisation_name;
org.organisation_number;

Here, org is the structure variable of the outer structure Organisation and emp is the structure variable of the inner structure Employee.

Different ways of nesting structure

The structure can be nested in the following different ways:

1. By separate nested structure: In this method, the two structures are created, but the dependent structure(Employee) is created outside the outer structure and is used inside the main structure(Organisation) as a member.

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

// Declaration of the
// dependent structure
struct Employee {
	int employee_id;
	char name[20];
	int salary;
};

// Declaration of the
// Outer structure
struct Organisation {
	char organisation_name[20];
	char org_number[20];

	// Dependent structure is used
	// as a member inside the main
	// structure for implementing
	// nested structure
	struct Employee emp;
};

int main() {
	struct Organisation org;

	printf("The size of structure organisation : %ld\n",
	       sizeof(org));

	org.emp.employee_id = 101;
	strcpy(org.emp.name, "Robert");
	org.emp.salary = 400000;
	strcpy(org.organisation_name,
	       "GeeksforGeeks");
	strcpy(org.org_number, "GFG123768");


	printf("Organisation Name : %s\n",
	       org.organisation_name);
	printf("Organisation Number : %s\n",
	       org.org_number);
	printf("Employee id : %d\n",
	       org.emp.employee_id);
	printf("Employee name : %s\n",
	       org.emp.name);
	printf("Employee Salary : %d\n",
	       org.emp.salary);
}

Output
The size of structure organisation : 68
Organisation Name : GeeksforGeeks
Organisation Number : GFG123768
Employee id : 101
Employee name : Robert
Employee Salary : 400000

2. By Embedded nested structure: In this method the nested structure is declared inside the outer structure, this method allows to declare structure inside a structure and requires fewer lines of code. 

Case 1: Error will occur if the structure is present but the structure variable is missing.

C
#include <stdio.h>

// Declaration of the outer
// structure
struct Organisation {
	char organisation_name[20];
	char org_number[20];

	// Declaration of the employee
	// structure
	struct Employee {
		int employee_id;
		char name[20];
		int salary;

		// This will cause error because
		// datatype struct is present ,
		// but Structure variable is missing.
	};
};

int main() {

	// Structure variable of organisation
	struct Organisation org;
	printf("%ld", sizeof(org.Employee.name));
}

Output:

./Solution.c: In function 'main':
./Solution.c:28:27: error: 'struct Organisation' has no member named 'Employee'
   printf("%ld", sizeof(org.Employee.name));

Note: Whenever an embedded nested structure is created, the variable declaration is compulsory at the end of the inner structure, which acts as a member of the outer structure. It is compulsory that the structure variable is created at the end of the inner structure. 

Case 2: When the structure variable of the inner structure is declared at the end of the inner structure. Below is the C program to implement this approach:

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

// Declaration of the main
// structure
struct Organisation {
	char organisation_name[20];
	char org_number[20];

	// Declaration of the dependent
	// structure
	struct Employee {
		int employee_id;
		char name[20];
		int salary;

		// variable is created which acts
		// as member to Organisation structure.
	} emp;
};

int main() {
	struct Organisation org;

	// Print the size of organisation
	// structure
	printf("The size of structure organisation : %ld\n",
	       sizeof(org));

	org.emp.employee_id = 101;
	strcpy(org.emp.name, "Robert");
	org.emp.salary = 400000;
	strcpy(org.organisation_name,
	       "GeeksforGeeks");
	strcpy(org.org_number, "GFG123768");


	printf("Organisation Name : %s\n",
	       org.organisation_name);
	printf("Organisation Number : %s\n",
	       org.org_number);
	printf("Employee id : %d\n",
	       org.emp.employee_id);
	printf("Employee name : %s\n",
	       org.emp.name);
	printf("Employee Salary : %d\n",
	       org.emp.salary);
}

Output
The size of structure organisation : 68
Organisation Name : GeeksforGeeks
Organisation Number : GFG123768
Employee id : 101
Employee name : Robert
Employee Salary : 400000

Accessing Nested Structure

Nested Structure can be accessed in two ways:

1. Using Normal variable

Outer and inner structure variables are declared as normal variables and the data members of the outer structure are accessed using a single dot(.) and the data members of the inner structure are accessed using the two dots. Below is the C program to implement this concept:

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

// Declaration of inner structure
struct college_details {
  int college_id;
  char college_name[50];
  
};

// Declaration of Outer structure
struct student_detail {
  int student_id;
  char student_name[20];
  float cgpa;
  
  // Inner structure variable
  struct college_details clg;
} stu;

int main() {
  struct student_detail stu = {12, "Kathy", 7.8, 
                              {14567, "GeeksforGeeks"}};
  
  // Printing the details
  printf("College ID : %d \n", stu.clg.college_id);
  printf("College Name : %s \n", stu.clg.college_name);
  printf("Student ID : %d \n", stu.student_id);
  printf("Student Name : %s \n", stu.student_name);
  printf("Student CGPA : %f \n", stu.cgpa);
  return 0;
}

Output
College ID : 14567 
College Name : GeeksforGeeks 
Student ID : 12 
Student Name : Kathy 
Student CGPA : 7.800000 

2. Using Pointer variable

One normal variable and one pointer variable of the structure are declared to explain the difference between the two. In the case of the pointer variable, a combination of dot(.) and arrow(->) will be used to access the data members. Below is the C program to implement the above approach:

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

// Declaration of inner structure
struct college_details {
	int college_id;
	char college_name[50];

};

// Declaration of Outer structure
struct student_detail {
	int student_id;
	char student_name[20];
	float cgpa;

	// Inner structure variable
	struct college_details clg;
} stu, *stu_ptr;

int main() {
	struct student_detail stu = {12, "Kathy", 7.8,
		{14567, "GeeksforGeeks"}
	};

	stu_ptr = &stu;

	printf("College ID : %d \n", stu_ptr->clg.college_id);
	printf("College Name : %s \n", stu_ptr->clg.college_name);
	printf("Student ID : %d \n", stu_ptr->student_id);
	printf("Student Name : %s \n", stu_ptr->student_name);
	printf("Student CGPA : %f \n", stu_ptr->cgpa);
	return 0;
}

Output
College ID : 14567 
College Name : GeeksforGeeks 
Student ID : 12 
Student Name : Kathy 
Student CGPA : 7.800000 

Drawback of Nested Structure

The drawback in nested structures are:

  • Independent existence not possible: It is important to note that structure Employee doesn't exist on its own. One can't declare structure variable of type struct Employee anywhere else in the program.
  • Cannot be used in multiple data structures: The nested structure cannot be used in multiple structures due to the limitation of declaring structure variables within the main structure. So, the most recommended way is to use a separate structure and it can be used in multiple data structures

Note: Nesting of the same structure within itself is not allowed. 

Example:

C
struct student
{
    char name[50];
    char address[100];
    int roll_no;
    struct student geek; // Invalid
}

This will cause error as we cannot nest a structure in itself.

Passing nested structure to function

A nested structure can be passed into the function in two ways:

  1. Pass the nested structure variable at once.
  2. Pass the nested structure members as an argument into the function.

Let's discuss each of these ways in detail.

1. Pass the nested structure variable at once: Just like other variables, a nested structure variable can also be passed to the function. Below is the C program to implement this concept:

C
#include <stdio.h>

// Declaration of the inner
// structure
struct Employee {
	int employee_id;
	char name[20];
	int salary;
};

// Declaration of the Outer
// structure
struct Organisation {
	char organisation_name[20];
	char org_number[20];

	// Nested structure
	struct Employee emp;
};

// Function show is expecting
// variable of outer structure
void show(struct Organisation);

int main() {
	struct Organisation org = {"GeeksforGeeks", "GFG111",
		{278, "Paul",5000}
	};

	// Organisation structure variable
	// is passed to function show
	show(org);
}

void show(struct Organisation org ) {
	printf("Printing the Details :\n");
	printf("Organisation Name : %s\n",
	       org.organisation_name);
	printf("Organisation Number : %s\n",
	       org.org_number);
	printf("Employee id : %d\n",
	       org.emp.employee_id);
	printf("Employee name : %s\n",
	       org.emp.name);
	printf("Employee Salary : %d\n",
	       org.emp.salary);
}

Output
Printing the Details :
Organisation Name : GeeksforGeeks
Organisation Number : GFG111
Employee id : 278
Employee name : Paul
Employee Salary : 5000

2. Pass the nested structure members as arguments into the function: Consider the following example to pass the structure member of the employee to a function display() which is used to display the details of an employee.

C
#include <stdio.h>

// Declaration of the inner
// structure
struct Employee
{
	int employee_id;
	char name[20];
	int salary;
};

// Declaration of the Outer
// structure
struct Organisation
{
	char organisation_name[20];
	char org_number[20];

	// Nested structure
	struct Employee emp;
};

// Function show is expecting
// members of both structures
void show(char organisation_name[], char org_number[],
int employee_id, char name[], int salary);

int main() {
	struct Organisation org = {"GeeksforGeeks", "GFG111",
	    {278, "Paul", 5000}};

	// Data members of both the structures
	// are passed to the function show
	show(org.organisation_name, org.org_number,
	org.emp.employee_id, org.emp.name, org.emp.salary);
}

void show(char organisation_name[], char org_number[],
    int employee_id, char name[], int salary) {
	printf("Printing the Details :\n");
	printf("Organisation Name : %s\n", organisation_name);
	printf("Organisation Number : %s\n", org_number);
	printf("Employee id : %d\n", employee_id);
	printf("Employee name : %s\n", name);
	printf("Employee Salary : %d\n", salary);
}

Output
Printing the Details :
Organisation Name : GeeksforGeeks
Organisation Number : GFG111
Employee id : 278
Employee name : Paul
Employee Salary : 5000

Next Article

Similar Reads