Display Data in Nested Structures - C Structure

C examples for Structure:Structure Value

Description

Display Data in Nested Structures

Demo Code

#include <stdio.h>

     struct date {
       int month;
       int day;/*from  w w w .  j av a 2s .  c om*/
       int year;
     };
     struct biodata {
       char name[15];
       int rollno;
       int age;
       float weight;
       struct date joiningDate;
     };
     
int main()
{

    struct biodata *ptr1, sa1 = {"Dick", 1, 21, 70.6F, 10, 18, 2006};
    struct biodata *ptr2, sa2 = {"Robert", 2, 22, 75.8F, 8, 24, 2007};
    ptr1 = &sa1;
    ptr2 = &sa2;
    
    printf("# 1: \n");
    printf("\tName: %s\n", (*ptr1).name);
    printf("\tRoll Number: %d\n", (*ptr1).rollno);
    printf("\tAge: %d years \n", (*ptr1).age);
    printf("\tWeight: %.1f kg\n", (*ptr1).weight);
    printf("\tJoining Date: %d/%d/%d\n\n", (*ptr1).joiningDate.month,
            (*ptr1).joiningDate.day, (*ptr1).joiningDate.year);
    
    printf("# 2: \n");
    printf("\tName: %s\n", ptr2->name);
    printf("\tRoll Number: %d\n", ptr2->rollno);
    printf("\tAge: %d years \n", ptr2->age);
    printf("\tWeight: %.1f kg\n", ptr2->weight);
    printf("\tJoining Date: %d/%d/%d\n", ptr2->joiningDate.month,
           ptr2->joiningDate.day, ptr2->joiningDate.year);

 return(0);
}

Result


Related Tutorials