Nested Structure in C with Examples

Last Updated : 16 Jul, 2026

A nested structure in C is a structure that is declared as a member of another structure. It allows related data to be organized hierarchically within a single structure.

  • A nested structure groups related information together, making complex data easier to organize and manage.
  • The members of the inner structure are accessed through the outer structure using the dot (`.`) or arrow (`->`) operator.
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

Syntax

struct Inner {
// Members of inner structure
};

struct Outer {
struct Inner member;
// Other members
};

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

  • The members of the outer structure are accessed using the dot (.) operator.
  • The members of the nested structure are accessed by chaining the dot (.) operator (for example, outer.inner.member).
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

  • A pointer to a structure can be used to access both the outer and nested structure members.
  • The arrow operator (->) is used to access structure members through the pointer, while the dot operator (.) is used for nested members.
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:

  • A nested structure declared inside another structure cannot be used independently or instantiated outside the enclosing structure.
  • Since it is limited to a single parent structure, it cannot be reused across multiple structures; declaring it separately improves reusability.

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

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. Passing the Entire Nested Structure to a Function

  • A nested structure variable can be passed directly to a function, just like any other structure variable.
  • This approach allows the function to access all members of the nested structure without passing each member individually.
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

  • Individual members of a nested structure can be passed as arguments to a function.
  • This approach is useful when the function only needs specific fields instead of the entire nested structure.
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
Comment