Nested structure in C programming

We can declare a structure inside another structure which is called nested structure in C language. C provides the facility to nesting a structure inside another. In this guide we will learn more about nested structure in C.

We can declare nested structure in C by 2 way.

  • Creating separate structure
  • Creating embedded structure

Creating separate structure in C

Let’s have a look at the following structure declaration where we have written the dependent structure inside the main structure as a member of independent structure.

// Creating separate structure in C

struct date_of_joining{
  int dd;
  int mm;
  int yyyy;
};

struct employee_struct{
  char name[20];
  int id;
  struct date_of_joining dt;   // nested structure
}em1;

Here, dt is a variable of type date_of_joining and we can use it as a member of other structure. Like this we can use date_of_joining structure inside many structure.

Creating embedded structure in C

In embedded structure we will declare a structure inside another structure. Declaring a structure inside another is called embedded structure. Consider the given example here.

// Creating embedded structure in C

struct employee_info{
    int id;
    char name[30];
    struct date_of_joining{    // nested structure
      int dd;
      int mm;
      int yyyy;
   }dt;
}e1;

Here, the structure dt is a nested structure of type date_of_joining which is inside the main structure employee_info.

How to access nested structure in C?

We can access a nested structure in C by the following syntax. consider above program to access its different member.

// Accessing nested structure

outer_structure.inner_structure.member

e1.dt.dd
e1.dt.mm
e1.dt.yyyy

In this above syntax, by e1.dt.dd we can access the dd member of inner structure. And by e1.dt.yyyy we can access the yyyy member of inner structure. Now, lets see an example program using nested structure in C.

C program using nested structure

Here, we will store the address of some entity employee in a structure.

// C program using nested structure

#include<stdio.h>
struct identity{
  int id;
  char city[30];
  char phone[14];
};

struct employee{
  char name[50];
  struct identity add;    // nested structure
};

int main(){
  struct employee em1;
  printf("Enter information of employee here : \n");
  printf("Enter name : ");
  scanf("%s", em1.name);

  printf("Enter city : ");
  scanf("%s", em1.add.city);

  printf("Enter phone no : ");
  scanf("%s", em1.add.phone);

  printf("Enter id : ");
  scanf("%d", &em1.add.id);

  printf("\nPrinting employee information......\n");
  printf("ID: %d\nName: %s\nCity: %s\nPhone: %s\n", em1.add.id, em1.name, em1.add.city, em1.add.phone);

  return 0;
}

Output of nested structure program:

Enter information of employee here :
Enter name : Alan
Enter city : Wasington
Enter phone no : 012345678910
Enter id : 302

Printing employee information......
ID: 302
Name: Alan
City: Wasington
Phone: 012345678910

Another C program using nested structure

// Example C program using nested structure

#include <stdio.h>
#include <string.h>
struct employee_struct{
  char name[30];
  int id;
  struct joining_date{
    int dd;
    int mm;
    int yyyy;
  }doj;
}e1;

int main(){
  e1.id = 23;
  strcpy(e1.name, "John Doe");
  e1.doj.dd = 29;
  e1.doj.mm = 12;
  e1.doj.yyyy = 2016;

  printf("Employee details ............. \n\n");
  printf("ID: %d\n", e1.id);
  printf("Name: %s\n", e1.name);
  printf("Joining date(dd/mm/yyyy): %d/%d/%d\n", e1.doj.dd,e1.doj.mm,e1.doj.yyyy);

  return 0;
}

Compile and run this program to get an output like this

nested structure in c, c program of nested structure

Previous pageStructure and function in C

Next pageC union