C arrow operator



We have seen different operators in c programming. In this tutorial we will be exploring more about arrow operator.

Arrow operator (->)

Arrow operator is used for accessing members of structure using pointer variable, below is the syntax of arrow operator in c programming –

Syntax of arrow operator

struct student
{
  char name[20],
  int roll;
}*ptr;

Expalanation :

Whenever we declare structure variable then member can be accessed using the dot operator. But when pointer to a structure is used then arrow operator is used.

Both dot and arrow operator serves same function to access member of structure. Lets compare dot operator and arrow operator –

struct student
{
  char name[20],
  int roll;
}std;
struct student
{
  char name[20],
  int roll;
}*ptr;
Access Structure MemberExample 1Example 2
Name is accessed usingstd.nameptr->name
Roll number is accessed usingstd.rollptr->roll

We can conclude that arrow operator is used to access the structure members when we use pointer variable to access it

In case if we want to access the members of structure using ordinary structure variable then we can use dot operator.

You can find nice explanation about the existence of arrow operator in c programming.

Arrow operator : Program

#include<stdio.h>
#include<malloc.h>

struct emp {
   int eid;
   char name[10];
}*ptr;

int main() {
   int i;

   printf("Enter the Employee Details : ");
   ptr = (struct emp *) malloc(sizeof(struct emp));

   printf("\nEnter the Employee ID : ");
   scanf("%d", &ptr->eid);
   printf("\nEnter the Employee Name : ");
   scanf("%s", ptr->name);

   printf("\nEmployee Details are : ");
   printf("\nRoll Number : %d", ptr->eid);
   printf("\nEmployee Name : %s", ptr->name);

   return (0);
}

Output :

Enter the Employee Details :
Enter the Employee ID : 1
Enter the Employee Name : Pritesh

Employee Details are :
Employee ID  : 1
Name         : Pritesh

Re-commanded Articles : Array of pointer to structure | Dynamic memory allocation