Nested Structure in C with Examples

07 Jan, 2022
Pre-requisite: Structures in C
A nested structure in C is a structure within structure. One structure can be declared inside another structure in the same way structure members are declared inside a structure.
Syntax:
struct name_1{    member1;   member2;   .   .   membern;
   struct name_2   {       member_1;       member_2;       .       .      member_n;   }, var1} var2;
The member of a nested structure can be accessed using the following syntax:
Variable name of Outer_Structure.Variable name of Nested_Structure.data member to access 
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:
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:
By separate nested structureBy embedded nested structure.
By separate nested structure
By embedded nested structure.
1. By separate nested structure: In this method, the two structures are created, but the dependent structure(Employee) should be used inside the main structure(Organisation) as a member. Below is the C program to implement the approach:
C
// C program to implement // the above approach#include <stdio.h>#include <string.h>  // Declaration of the // dependent structurestruct Employee{  int employee_id;  char name[20];  int salary;};  // Declaration of the // Outer structurestruct 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; };  // Driver codeint main(){  // Structure variable  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");          // Printing the details  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 : 68Organisation Name : GeeksforGeeksOrganisation Number : GFG123768Employee id : 101Employee name : RobertEmployee Salary : 400000
2. By Embedded nested structure: Using this method, allows to declare structure inside a structure and it requires fewer lines of code. 
Case 1: Error will occur if the structure is present but the structure variable is missing.
C
// C program to implement // the above approach#include <stdio.h>  // Declaration of the outer// structurestruct 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 line will cause error because   // datatype struct Employee is present ,  // but Structure variable is missing.  }; };  // Driver codeint main(){  // Structure variable of organisation  struct Organisation org;  printf("%ld", sizeof(org));}
Output:
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
// C program to implement// the above approach#include <stdio.h>#include <string.h>  // Declaration of the main// structurestruct 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; };  // Driver codeint 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");          // Printing the details  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 : 68Organisation Name : GeeksforGeeksOrganisation Number : GFG123768Employee id : 101Employee name : RobertEmployee Salary : 400000
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
C Nested Structure Example
Below is another example of a C nested structure.
C
// C program to implement // the nested structure#include <stdio.h>#include <string.h>  // Declaration of Outer structurestruct 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;};     // Driver codeint 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 : GeeksforGeeksRanking : 7Student id : 111Student name : PaulRoll no : 278
Note:
Nesting of structure within itself is not allowed. 
Example:
struct student{
    char name[50];
    char address[100];
    int roll_no;
    struct student geek; // Invalid
}
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.
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
// C program to implement// the above approach#include <stdio.h>  // Declaration of the inner // structurestruct Employee {  int employee_id;  char name[20];  int salary;};  // Declaration of the Outer // structurestruct Organisation {  char organisation_name[20];  char org_number[20];      // Nested structure  struct Employee emp; };  // Function show is expecting // variable of outer structurevoid show(struct Organisation);  // Driver codeint main(){  struct Organisation org = {"GeeksforGeeks", "GFG111",                            {278, "Paul",5000}};      // Organisation structure variable   // is passed to function show  show(org);}  // Function shoe definitionvoid show(struct Organisation org ){  // Printing the details  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 : GeeksforGeeksOrganisation Number : GFG111Employee id : 278Employee name : PaulEmployee 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
// C program to implement// the above approach#include <stdio.h>  // Declaration of the inner // structurestruct Employee {  int employee_id;  char name[20];  int salary;};  // Declaration of the Outer // structurestruct Organisation {  char organisation_name[20];  char org_number[20];      // Nested structure  struct Employee emp; };  // Function show is expecting // members of both structuresvoid show(char organisation_name[],           char org_number[],          int employee_id,           char name[], int salary);  // Driver codeint 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);}  // Function show definitionvoid show(char organisation_name[],           char org_number[],          int employee_id,           char name[], int salary){  // Printing the details  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 : GeeksforGeeksOrganisation Number : GFG111Employee id : 278Employee name : PaulEmployee Salary : 5000
Accessing Nested Structure
Nested Structure can be accessed in two ways:
Using Normal variable.Using Pointer variable.
Using Normal variable.
Using Pointer variable.
Let’s discuss each of these methods in detail.
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
// C program to implement// the above approach#include <stdio.h>#include <string.h>  // Declaration of inner structurestruct college_details{  int college_id;  char college_name[50];    };  // Declaration of Outer structurestruct student_detail{  int student_id;  char student_name[20];  float cgpa;      // Inner structure variable  struct college_details clg;} stu;  // Driver codeint 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
// C program to implement// the above approach#include <stdio.h>#include <string.h>  // Declaration of inner structurestruct college_details{  int college_id;  char college_name[50];    };  // Declaration of Outer structurestruct student_detail{  int student_id;  char student_name[20];  float cgpa;      // Inner structure variable  struct college_details clg;} stu, *stu_ptr;  // Driver codeint main(){  struct student_detail stu = {12, "Kathy", 7.8,                               {14567, "GeeksforGeeks"}};      stu_ptr = &stu;      // Printing the details  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 
A nested structure will allow the creation of complex data types according to the requirements of the program.
sweetyty
surindertarika1234
sagar0719kumar
simmytarika5
C-Structure & Union
C Language
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Unordered Sets in C++ Standard Template Library
Operators in C / C++
Exception Handling in C++
What is the purpose of a function prototype?
TCP Server-Client implementation in C
Smart Pointers in C++ and How to Use Them
'this' pointer in C++
Storage Classes in C
Ways to copy a vector in C++
Understanding "extern" keyword in C