Arrow operator -> in C/C++ with Examples - GeeksforGeeks

06 Sep, 2021
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 greater 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++
C
// C++ program to show Arrow operator// used in structure#include <iostream>using namespace std; // Creating the structurestruct student {    char name[80];    int age;    float percentage;}; // Creating the structure objectstruct student* emp = NULL; // Driver codeint main(){       // Assigning memory to struct variable emp    emp = (struct student*)        malloc(sizeof(struct student));     // Assigning value to age variable    // of emp using arrow operator    emp->age = 18;     // Printing the assigned value to the variable    cout <<" "<< emp->age;     return 0;} // This code is contributed by shivanisinghss2110
// C program to show Arrow operator// used in structure #include <stdio.h>#include <stdlib.h> // Creating the structurestruct student {    char name[80];    int age;    float percentage;}; // Creating the structure objectstruct student* emp = NULL; // Driver codeint main(){    // Assigning memory to struct variable emp    emp = (struct student*)        malloc(sizeof(struct student));     // Assigning value to age variable    // of emp using arrow operator    emp->age = 18;     // Printing the assigned value to the variable    printf("%d", emp->age);     return 0;}
18
 
Arrow operator in unions:
C++
C
// C++ program to show Arrow operator// used in structure#include <iostream>using namespace std; // Creating the unionunion student {    char name[80];    int age;    float percentage;}; // Creating the union objectunion student* emp = NULL; // Driver codeint main(){    // Assigning memory to struct variable emp    emp = (union student*)        malloc(sizeof(union student));     // Assigning value to age variable    // of emp using arrow operator    emp->age = 18;     // DIsplaying the assigned value to the variable    cout <<""<< emp->age;} // This code is contributed by shivanisinghss2110
// C program to show Arrow operator// used in structure #include <stdio.h>#include <stdlib.h> // Creating the unionunion student {    char name[80];    int age;    float percentage;}; // Creating the union objectunion student* emp = NULL; // Driver codeint main(){    // Assigning memory to struct variable emp    emp = (union student*)        malloc(sizeof(union student));     // Assigning value to age variable    // of emp using arrow operator    emp->age = 18;     // DIsplaying the assigned value to the variable    printf("%d", emp->age);}
18
 
tanvibugdani
shivanisinghss2110
C Language
C Programs
Competitive Programming
Program Output
Programming Language
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Multidimensional Arrays in C / C++
rand() and srand() in C/C++
fork() in C
Core Dump (Segmentation fault) in C/C++
Left Shift and Right Shift Operators in C/C++
Strings in C
UDP Server-Client implementation in C
C Program to read contents of Whole File
Header files in C/C++ and its uses
How to return multiple values from a function in C or C++?