Embedded Structure - Devbhoomi

FREE JOB ALERT & ONLINE TUTORIALS

Hot

Post Top Ad

Sunday 5 November 2017

Embedded Structure

We can define structure within the structure also.

    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 nested structure by Outer_Structure.Nested_Structure.member as given below :

    emp1.doj.dd
    emp1.doj.mm
    emp1.doj.yyyy


Example of nested structures

    #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=1;
    //copying string into char array
    strcpy(e1.name, "xyz");

    e1.doj.dd=26;
    e1.doj.mm=12;
    e1.doj.yyyy=2014;

    //printing 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;
    }

Output :
employee id : 1
employee name : xyz
employee date of joining (dd/mm/yyyy) : 26/12/2014

No comments:

Post a Comment

Post Top Ad