English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

C Language Basic Tutorial

C Language Flow Control

C Language Functions

C Language Arrays

C Language Pointers

C Language Strings

C Language Structures

C Language Files

C Others

C Language Reference Manual

C Language Nested Structures

C provides us with the functionality to nest one structure inside another, thus creating complex data types. For example, we may need to store the address of an actual employee in the structure. The address can also contain other information, such as street number, city, district, and zip code. Therefore, to store the employee's address, we need to store the employee's address in a separate structure and nest the address of this structure into the employee's structure. See the following program.

#include<stdio.h>
struct address 
{
    char city[20];
    int pin;
    char phone[14];
};
struct employee
{
    char name[20];
    struct address add;
};
void main()
{
    struct employee emp;
    printf("Enter employee information?\n");
    scanf("%s %s %d %s", emp.name, emp.add.city, &emp.add.pin, emp.add.phone);
    printf("Print employee information....\n");
    printf("Name: %s\nCity: %s\nPNumber: %d\nPhone: %s", emp.name, emp.add.city, emp.add.pin, emp.add.phone);
}

Output result

Enter employee information?
Arun            
Delhi           
110001       
1234567890    
Print employee information....   
Name: Arun      
City: Delhi  
Number: 110001
Phone: 1234567890

The structure can be nested in the following way.

  1. Through separate structures

  2. Through embedded structures

1)Separate structures

In this example, we have created two structures, but the subordinate structure should be used as a member within the main structure. See the following example.

struct Date
{
   int dd;
   int mm;
   int yyyy; 
};
struct Employee
{   
   int id;
   char name[20];
   struct Date doj;
}}emp1;

It can be seen that doj is a variable of date type. Here, doj is used as a member of the Employee structure. In this way, we can use the Date structure in many structures.

2)Embedded structure

Embedded structures allow us to declare structures within a structure. Therefore, it requires fewer lines of code, but it cannot be used in multiple data structures. See the following example.

struct Employee
{   
   int id;
   char name[20];
   struct Date
    {
      int dd;
      int mm;
      int yyyy; 
    })doj;
}}emp1;

Accessing Nested Structure

We can access the members of a nested structure using Outer_Structure.Nested_Structure.member, as shown below:

e1.doj.dd
e1.doj.mm
e1.doj.yyyy

C Nested Structure Example

Let's look at a simple example of nested structures in C language.

#include<stdio.h>
#include<string.h>
struct Employee
{   
   int id;
   char name[20];
   struct Date
    {
      int dd;
      int mm;
      int yyyy; 
    })doj;
})e1;
int main(
{
   //Store Employee Information
   e1.id =101;
   strcpy(e1.name, "Sonoo Jaiswal");//Copy a string to a character array
   e1.doj.dd =10;
   e1.doj.mm =11;
   e1.doj.yyyy =2014;
   //Print First Employee Information
   printf("Employee ID : %d\n", e1.id);
   printf("Employee Name : %s\n", e1.name);
   printf("Date of Joining Employee : (dd/mm/yyyy : %d/%d/%d\n", e1.doj.dd,e1.doj.mm,e1.doj.yyyy);
   return 0;
}

Output :

Employee ID : 101
Employee Name : Seagull Ali
Date of Joining Employee : (dd/mm/yyyy : 10/11/2014

Passing Structure to Function

Like other variables, structures can also be passed to functions. We can pass structure members to functions, or pass the structure variable itself. The following example passes the structure variable employee to the display() function, which is used to display the employee's details.

#include<stdio.h>
struct address 
{
    char city[20];
    int pin;
    char phone[14];
};
struct employee
{
    char name[20];
    struct address add;
};
void display(struct employee)
void main()
{
    struct employee emp;
    printf("Enter employee information?\n");
    scanf("%s %s %d %s", emp.name, emp.add.city, &emp.add.pin, emp.add.phone);
    display(emp);
}
void display(struct employee emp)
{
  printf("Print employee information.....\n");
  printf("%s %s %d %s",emp.name,emp.add.city,emp.add.pin,emp.add.phone);
}