Structure with Pointers in C Language | Declaring pointer variable of Structure in C Language
We can declare pointer variables of a structure just like other variables. We can declare structure type pointer variable and store address of another structure variable in it.
Syntax :
Struct_name *Struct_pointer;
Here, Struct_name refers to the name of structure which has been defined whose variable we want to declare.
Struct_pointer refers the name of pointer variable of structure type. The name of structure variable should follow all the rules of naming an identifier in C language.
We can access structure elements through its pointer variable in two ways:
– Using arrow operator(->)
– Using dot operator (.)
Syntax to declare structure type pointer variable :
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