Pointer to Structure in C
What is Pointer to Structure in C?
Pointer to structure means when we declare a pointer variable of type structure, and we know that structure ia also a data type which is user defined.By using pointer to structure we can read and write the data of structure.Here we use -> (arrow operator) to access the data of structure using pointer.
Syntax to declare variable using Pointer to Structure
struct structureName
{
dataType member1;
dataType member2;
dataType member3;
...
} *variable_name;
Another way of creating variable using Pointer to structure
struct structureName
{
dataType member1;
dataType member2;
dataType member3;
...
};
void main()
{
struct structureName *variable_name;
}
Example:
#include<stdio.h>
struct student
{
int rollno;
float fee;
char name[10];
};
void main()
{
struct student st;
struct student *ptr; //pointer of structure
ptr=&st;
printf("Enter rollno ");
scanf("%d",&ptr->rollno);
printf("Enter fee ");
scanf("%f",&ptr->fee);
printf("Enter name ");
scanf("%s",&ptr->name);
printf("\nRollno is %d",ptr->rollno);
printf("\nFee is %f",ptr->fee);
}//end of main
Output
Enter rollno 10
Enter fee 10000
Enter name sunil
Rollno is 10
Fee is 10000.000000
Name is sunil