Structure with Pointers in C

Preview
You must first complete Introduction to Structure in C before viewing this Lesson

Structure with Pointers in C

Just like other data types, we can also declare pointer variables of a structure. 

The syntax for declaring a structure type pointer variable is:

struct Struct_name  *Struct_pointer;

Struct_pointer refers the name of pointer variable of structure type.

We can access structure elements through its pointer variable in two ways:

  • Using arrow operator(->)
  • Using dot operator (.)



Program to demonstrate use of pointer variable of a structure by using arrow (->) operator.
#include<string.h>
#include<stdio.h>
struct student
{
int rollno;
float fees;
};
int main()
{
struct student st={131,550.50};
struct student *p1;
p1=&st;
printf(“\nRollno=%d”,p1->rollno);     //-> arrow operator
printf(“\nFees=%f”,p1->fees);return(0);
}
Output
Rollno=131
Fees=550.500000



Program to demonstrate use of pointer variable of a structure by using dot (.) operator.
#include<string.h>
#include<stdio.h>
struct student
{
int rollno;
float fees;
};
int main()
{
struct student st={131,550.50};
struct student *p1;
p1=&st;
printf(“\nRollno=%d”,(*p1).rollno);     //-> arrow operator
printf(“\nFees=%f”,(*p1).fees);

return(0);
}
Output
Rollno=131
Fees=550.500000



Best Books of C





Lesson tags: arrow operator in c, operators used with structure pointer, structure pointer in c, structure type pointer variable, use of dot operator in c
Back to: C Programming Language