An Arrow operator in C/C++ allows to access elements in Structures and Unions. It is used with a pointer variable pointing to a structure or union. The arrow operator is formed by using a minus sign, followed by the geater than symbol as shown below.
Syntax:
(pointer_name)->(variable_name)
Operation: The -> operator in C or C++ gives the value held by variable_name to structure or union variable pointer_name.
Difference between Dot(.) and Arrow(->) operator:
- The Dot(.) operator is used to normally access members of a structure or union.
- The Arrow(->) operator exists to access the members of the structure or the unions using pointers.
Examples:
- Arrow operator in structure:
// C program to show Arrow operator// used in structure#include <stdio.h>#include <stdlib.h>// Creating the structurestructstudent {charname[80];intage;floatpercentage;};// Creating the structure objectstructstudent* emp = NULL;// Driver codeintmain(){// Assigning memory to struct variable empemp = (structstudent*)malloc(sizeof(structstudent));// Assigning value to age variable// of emp using arrow operatoremp->age = 18;// Printing the assigned value to the variableprintf("%d", emp->age);return0;}Output:18
- Arrow operator in unions:
// C program to show Arrow operator// used in structure#include <stdio.h>#include <stdlib.h>// Creating the unionunionstudent {charname[80];intage;floatpercentage;};// Creating the union objectunionstudent* emp = NULL;// Driver codeintmain(){// Assigning memory to struct variable empemp = (unionstudent*)malloc(sizeof(unionstudent));// Assigning value to age variable// of emp using arrow operatoremp->age = 18;// DIsplaying the assigned value to the variableprintf("%d", emp->age);}Output:18
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price.
In case you wish to attend live classes with industry experts, please refer Geeks Classes Live


