C Nested Structure

Structure can contain other Structure variables as members. This is called Nesting of Structures.

For example address structure is a structure. We can define a nested structure called customer which contains address structure as follows:

snippet
struct address {
    int house_number;
    char street_name[50];
    int zip_code;
    char country[50];
};
struct customer {
    char name[50];
    structure address billing_addr;
    structure address shipping_addr;
};
struct customer cust1;

If the structure contains another structure, we can use dot operator to access nested structure and use dot operator again to access variables of nested structure.

cust1.billing_addr.house_number = 10;

It should be noted that a structure definition can’t contain the variable of its own type as a data member. This means the following structure definition is illegal.

snippet
struct address {
    int house_number;
    char street_name[50];
    int zip_code;
    char country[50];
    struct address old_address;
    /* The statement above is illegal*/
};

Consider another example. To store the address of an employee in a structure, store the attributes of address such as street number, city, state, and pin code of the employee into a separate structure and nest the structure address into the structure employee .Consider the following program.

snippet
#
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("Printing the employee information....\n");
    printf("name: %s\nCity: %s\nPincode: %d\nPhone: %s", emp.name, emp.add.city, emp.add.pin, emp.add.phone);
}
Enter employee information?
Arun            
Delhi           
110001       
1234567890    

Printing the employee information....   
name: Arun      
City: Delhi  
Pincode: 110001
Phone: 1234567890

The structure can be nested in the following ways.

  1. By separate structure
  2. By Embedded structure

1) Separate structure

Here, we create two structures, but the dependent structure should be used inside the main structure as a member.

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

As you can see, doj (date of joining) is the variable of type Date. Here doj is used as a member in Employee structure. In this way, we can use Date structure in many structures.

2) Embedded structure

The embedded structure enables us to declare the structure inside the structure. Hence, it requires less line of codes but it can not be used in multiple data structures. Consider the following example.

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

Accessing Nested Structure

We can access the member of the nested structure by Outer_Structure.Nested_Structure.member as given below:

  1. e1.doj.dd
  2. e1.doj.mm
  3. e1.doj.yyyy

C Nested Structure example

Let's see a simple example of the nested structure in C language.

snippet
#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( )  
{  
   //storing employee information  
   e1.id=101;  
   strcpy(e1.name, "Sonoo Jaiswal");//copying string into char array  
   e1.doj.dd=10;  
   e1.doj.mm=11;  
   e1.doj.yyyy=2014;  
  
   //printing first employee information  
   printf( "employee id : %d\n", e1.id);  
   printf( "employee name : %s\n", e1.name);  
   printf( "employee date of joining (dd/mm/yyyy) : %d/%d/%d\n", e1.doj.dd,e1.doj.mm,e1.doj.yyyy);  
   return 0;  
}  
employee id : 101
employee name : John Abraham
employee date of joining (dd/mm/yyyy) : 10/11/2014

Passing structure to function

Similar to other variables, a structure can also be passed to a function. In the below example pass the structure variable employee to a function display() which is used to display the details of an employee.

snippet
#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("Printing the details....\n");  
  printf("%s %s %d %s",emp.name,emp.add.city,emp.add.pin,emp.add.phone);  
}  
Related Tutorial
Follow Us
https://www.facebook.com/Rookie-Nerd-638990322793530 https://twitter.com/RookieNerdTutor https://plus.google.com/b/117136517396468545840 #
Contents +