Example
// C program to understand how to access members through pointers in structure type variable
#include <stdio.h>
struct student {
char name;
int *roll;
char gender;
int marks[5];
};
int main() {
int Rohanroll = 54;
struct student stu = { “Rohan”, &RohanRoll, ‘M’, { 79, 98, 86, 68, 82 }};
printf (“Name: %s\n”, stu.name);
printf (“Roll: %d\n”, *(stu.roll));
printf (“Gender: %c\n”, stu.gender);
for ( int i = 0; i < 5; ++i)
printf (“Marks in subject – %d is: %d\n”, i+1, stu.marks[i]);
return 0;
}
Output:
Name: Rohan
Roll: 54
Gender: M
Marks in subject – 1 is: 79
Marks in subject – 2 is: 98
Marks in subject – 3 is: 86
Marks in subject – 4 is: 68
Marks in subject – 5 is: 82
Explanation: Using “.” (the dot operator), we can again access the members. Since roll now has the address of RohanRoll, we will have to dereference stu.roll to get the value (and not stu1.(*roll) ).