Nested Structure in C with Examples
Last Updated :
21 May, 2025
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;
}
OutputCollege 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);
}
OutputThe 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);
}
OutputThe 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;
}
OutputCollege 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;
}
OutputCollege 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:
- Pass the nested structure variable at once.
- 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);
}
OutputPrinting 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);
}
OutputPrinting the Details :
Organisation Name : GeeksforGeeks
Organisation Number : GFG111
Employee id : 278
Employee name : Paul
Employee Salary : 5000
Similar Reads
C Programming Language Tutorial C is a general-purpose mid-level programming language developed by Dennis M. Ritchie at Bell Laboratories in 1972. It was initially used for the development of UNIX operating system, but it later became popular for a wide range of applications. Today, C remains one of the top three most widely used
5 min read
Dynamic Memory Allocation in C using malloc(), calloc(), free() and realloc() In C, a variable defined in a function is stored in the stack memory. The requirement of this memory is that it needs to know the size of the data to memory at compile time (before the program runs). Also, once defined, we can neither change the size nor completely delete the memory.To resolve this,
9 min read
Data Types in C Each variable in C has an associated data type. It specifies the type of data that the variable can store like integer, character, floating, double, etc.Example:C++int number;The above statement declares a variable with name number that can store integer values.C is a statically type language where
5 min read
C Language Introduction C is a general-purpose procedural programming language initially developed by Dennis Ritchie in 1972 at Bell Laboratories of AT&T Labs. It was mainly created as a system programming language to write the UNIX operating system.Main features of CWhy Learn C?C is considered mother of all programmin
6 min read
C Arrays An array in C is a fixed-size collection of similar data items stored in contiguous memory locations. It can be used to store the collection of primitive data types such as int, char, float, etc., as well as derived and user-defined data types such as pointers, structures, etc. Creating an Array in
7 min read
C Pointers A pointer is a variable that stores the memory address of another variable. Instead of holding a direct value, it has the address where the value is stored in memory. This allows us to manipulate the data stored at a specific memory location without actually using its variable. It is the backbone of
9 min read
C Programs To learn anything effectively, practicing and solving problems is essential. To help you master C programming, we have compiled over 100 C programming examples across various categories, including basic C programs, Fibonacci series, strings, arrays, base conversions, pattern printing, pointers, and
8 min read
Basics of File Handling in C File handling in C is the process in which we create, open, read, write, and close operations on a file. C language provides different functions such as fopen(), fwrite(), fread(), fseek(), fprintf(), etc. to perform input, output, and many different C file operations in our program.Need of File Han
13 min read
Operators in C In C language, operators are symbols that represent some kind of operations to be performed. They are the basic components of the C programming. In this article, we will learn about all the operators in C with examples.What is an Operator in C?A C operator can be defined as the symbol that helps us
11 min read
Bitwise Operators in C In C, bitwise operators are used to perform operations directly on the binary representations of numbers. These operators work by manipulating individual bits (0s and 1s) in a number.The following 6 operators are bitwise operators (also known as bit operators as they work at the bit-level). They are
6 min read