Structure with Pointers in C Language | Declaring pointer variable of Structure in C Language

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

Spread the love
Lesson tags: arrow operator in c, Declaring pointer variable of Structure in C Language, operators used with structure pointer, structure pointer in c, structure type pointer variable, Structure with Pointers in C Language, use of dot operator in c
Back to: C Programming Language
Spread the love