Table of Contents

    Nested Structures in C: Usage and Examples

    Nested Structures in C: Usage and Examples

    Nested structure in C is nothing but structure within a structure. One structure can be declared inside other structure as we declare structure members inside a structure. The structure variables can be a normal structure variable or a pointer variable to access the data. You can learn the concepts in this section.

    Structure within structure in C using normal variable

    This program explains how to use structure within structure in C using normal variable. university_detail structure is declared inside student_detail structure in this program. Both structure variables are normal structure variables.

    Please note that members of university_detail structure are accessed by 2 dot(.) operator and members of student_detail structure are accessed by single dot(.) operator.

    #include 
    #include 
     
    struct university_detail
    {
        int u_id;
        char u_name[50];
    };
     
    struct student_detail 
    {
        int s_id;
        char s_name[20];
        float s_marks;
        // structure within structure
        struct university_detail u_data;
    }stu_data;
     
    int main() 
    {
        struct student_detail stu_data = {1, "Rambo", 99.5, 89145,
                                           "Peace University"};
        printf(" Id is: %d \n", stu_data.s_id);
        printf(" Name is: %s \n", stu_data.s_name);
        printf(" Percentage is: %f \n\n", stu_data.s_marks);
     
        printf(" College Id is: %d \n", 
                        stu_data.u_data.u_id);
        printf(" College Name is: %s \n", 
                        stu_data.u_data.u_name);
        return 0;
    }
    Id is: 1
     Name is: Rambo
     Percentage is: 99.500000
    
     College Id is: 89145
     College Name is: Peace University
    Press any key to continue . . .

    Structure within structure in C using pointer variable

    This program explains how to use structure within structure in C using pointer variable. university_detail structure is declared inside student_detail structure in this program. one normal structure variable and one pointer structure variable is used in this program.

    Please note that combination of .(dot) and ->(arrow) operators are used to access the structure member which is declared inside the structure.

    Example Program

    #include 
    #include 
     
    struct university_detail
    {
        int u_id;
        char u_name[50];
    };
     
    struct student_detail 
    {
        int s_id;
        char s_name[20];
        float s_marks;
        // structure within structure
        struct university_detail u_data;
    }stu_data, *stu_data_ptr;
     
    int main() 
    {
        struct student_detail stu_data = {21, "Rambo", 99.5, 89145,
                                           "Peace University"};
                                           
        stu_data_ptr = &stu_data;                                  
                                           
        printf(" Id is: %d \n", stu_data_ptr->s_id);
        printf(" Name is: %s \n", stu_data_ptr->s_name);
        printf(" Percentage is: %f \n\n", 
                             stu_data_ptr->s_marks);
     
        printf(" College Id is: %d \n", 
                             stu_data_ptr->u_data.u_id);
        printf(" College Name is: %s \n", 
                          stu_data_ptr->u_data.u_name);
        return 0;
    }

    Output

    Id is: 21
     Name is: Rambo
     Percentage is: 99.500000
    
     College Id is: 89145
     College Name is: Peace University
    Press any key to continue . . .