DUMMY_CHAPTER_NAME

Structure within structure in C using pointer variable

Using Structure - C Programming

Structure within structure in C using pointer variable

This program explains how to use structure within structure using pointer variable. "college_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.


#include <stdio.h>

#include <string.h>

struct college_detail

{ int college_id;

char college_name[50];

};

struct student detail

{ int sno;

char name[20];

float percentage;

/*structure within structure */

struct college_detail_clg_data;

} stu_data, *stu_data_ptr;

int main()

{

struct detail stu_data={1, "Mahi", 98.5, 13813, Anna University"};

stu_data_ptr=&stu_data;

printf("Sno is: %d\n", stu_data_ptr‒>sno);

printf("Name is: %s\n", stu_data_ptr‒>name);

printf("Percentage is: %f\n\n", stu_data_ptr‒>percentage);

printf("College Id is: %d\n",stu_data_ptr‒>clg_data.college_sno);

printf("College Name is: %s\n", stu_data_ptr‒>clg_data.college_name);

return 0;

}

OUTPUT

Sno is : 1

Name is : Mahi

Percentage is : 98.500000

College Id is : 13813

College Name is : Anna University

 


DUMMY_CHAPTER_NAME : Tag: : - Structure within structure in C using pointer variable


DUMMY_CHAPTER_NAME



Under Subject



Related Subjects