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

Last Updated : 29 Jun, 2026

The arrow (->) operator is used to access members of a Structures or Unions through a pointer. It combines pointer dereferencing and member access into a single operator.

  • Used when a pointer points to a structure or union instead of a direct variable.
  • It is a shorthand for (*pointer).member, making the code simpler and more readable.
C++
#include <iostream>

struct Student {
    int id;
};

int main() {
    Student s = {101};
    Student *ptr = &s;

    std::cout << ptr->id;

    return 0;
}
C
#include <stdio.h>

struct Student {
    int id;
};

int main() {
    struct Student s = {101};
    struct Student *ptr = &s;

    printf("%d", ptr->id);

    return 0;
}

Output
101

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.

Examples

some examples of Arrow operator are:

Arrow operator in structure

C++
// C++ program to show Arrow operator
// used in structure
#include <iostream>
using namespace std;

// Creating the structure
struct student {
    char name[80];
    int age;
    float percentage;
};

// Creating the structure object
struct student* emp = NULL;

// Driver code
int 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
// C program to show Arrow operator
// used in structure

#include <stdio.h>
#include <stdlib.h>

// Creating the structure
struct student {
    char name[80];
    int age;
    float percentage;
};

// Creating the structure object
struct student* emp = NULL;

// Driver code
int 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;
}
JavaScript
// JavaScript program to show Arrow operator used in structure
let emp = null;

// Creating the structure
class Student {
    constructor(name, age, percentage) {
        this.name = name;
        this.age = age;
        this.percentage = percentage;
    }
}

// Assigning memory to struct variable emp
emp = new Student('', 0, 0);

// Assigning value to age variable
// of emp using arrow operator
emp.age = 18;

// Printing the assigned value to the variable
console.log(' ' + emp.age);

Output
18

Arrow operator in unions

C++
// C++ program to show Arrow operator
// used in structure
#include <iostream>
using namespace std;

// Creating the union
union student {
    char name[80];
    int age;
    float percentage;
};

// Creating the union object
union student* emp = NULL;

// Driver code
int 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
// C program to show Arrow operator
// used in structure

#include <stdio.h>
#include <stdlib.h>

// Creating the union
union student {
    char name[80];
    int age;
    float percentage;
};

// Creating the union object
union student* emp = NULL;

// Driver code
int 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);
}
Comment