Nested Structure

In nested structure one structure can be declared inside other structure as we declare structure members inside a structure. Structure variable can be normal structure variable or pointer variable to access the data.Hence, we store the address of the employee and we need to store the address of the employee into a separate structure.

Let’s 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("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);  
}

Output

Enter employee information?

Anika            

Hyderabad           

110001       

1234567890    

Printing the employee information....   

name: Anika      

City: Hyderabad  

Pincode: 110001

Phone: 1234567890

Nested Structure Example

Let’s see the example of the nested structure

#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( )  
{    
   e1.id=101;  
   strcpy(e1.name, "Neha Jaiswal");  
   e1.doj.dd=10;  
   e1.doj.mm=10;  
   e1.doj.yyyy=2019;  
  
   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;  
}

Output

employee id : 101
employee name : Neha Jaiswal
employee date of joining (dd/mm/yyyy) : 10/10/2019

Submit your review
1
2
3
4
5
Submit
     
Cancel

Create your own review

The Technical Funda
Average rating:  
 0 reviews