Structure with Pointers in C

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



Structure with Pointers in C

Syntax to declare structure type pointer variable is:

struct Struct_name  *var;

var represents pointer variable of structure.

We can use pointer variable of structure to access its elements pointer in two ways:

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



Program to demonstrate use of pointer variable of a structure.
#include<stdio.h>
struct emp
{
int empid;
float sal;
};
int main()
{
struct emp e1={101,50000.00};
struct emp *e2;
e2=&e1;
printf(“\nEmpid=%d”,e2->empid); 
printf(“\nSalary=%f”,(*e2).sal);

 

return(0);
}

Output
Empid=101
Salary=50000.00



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