30. Pointers to Structures

By | November 13, 2020

Dot(.) operator is used to access the data using normal structure variable and arrow (->) is used to access the data using a pointer variable. You have learned how to access structure data using normal variables in C – Structure topic. So, we are showing here how to access structure data using the pointer variable in the below C program.

EXAMPLE PROGRAM FOR C STRUCTURE USING POINTER:

In this program, “record1” is a normal structure variable and “ptr” is the pointer structure variable. As you know, Dot(.) operator is used to access the data using the normal structure variable, and arrow(->) is used to access data using the pointer variable.

#include <stdio.h>

#include <string.h> 

struct student {    

int id;    

char name[30];    

float percentage;

};

int main() {    

int i;    

struct student record1 = {1, “Raju”, 90.5};    

struct student *ptr;     

ptr = &record1;              

printf(“Records of STUDENT1: \n”);        

printf(”  Id is: %d \n”, ptr->id);        

printf(”  Name is: %s \n”, ptr->name);        

printf(”  Percentage is: %f \n\n”, ptr->percentage);     

return 0;

}

OUTPUT:

Records of STUDENT1:
Id is: 1
Name is: Raju
Percentage is: 90.500000

Leave a Reply

Your email address will not be published. Required fields are marked *