What is the '-->' operator in C/C++?

In C and C++, the -> operator is used for accessing members of a structure or class through a pointer. It is not the same as the . operator, which is used to access members directly from an object. The -> operator combines dereferencing a pointer and accessing a member in one step.

Syntax and Usage

Syntax:

pointer->member

Usage:

  • pointer is a pointer to a structure or class.
  • member is a member of the structure or class.

When you have a pointer to a structure or a class, you use the -> operator to access its members instead of using the dereference operator * followed by the dot operator ..

Example with Structures

Consider the following structure in C:

#include <stdio.h> struct Person { char name[50]; int age; }; int main() { struct Person person = {"John Doe", 30}; struct Person *ptr = &person; // Accessing members using the pointer and the -> operator printf("Name: %s\n", ptr->name); printf("Age: %d\n", ptr->age); return 0; }

In this example:

  • ptr is a pointer to a Person structure.
  • ptr->name accesses the name member of the structure pointed to by ptr.
  • ptr->age accesses the age member of the structure pointed to by ptr.

Example with Classes

In C++, the -> operator is used similarly with classes:

#include <iostream> #include <string> class Person { public: std::string name; int age; Person(std::string n, int a) : name(n), age(a) {} }; int main() { Person person("John Doe", 30); Person *ptr = &person; // Accessing members using the pointer and the -> operator std::cout << "Name: " << ptr->name << std::endl; std::cout << "Age: " << ptr->age << std::endl; return 0; }

In this example:

  • ptr is a pointer to a Person object.
  • ptr->name accesses the name member of the object pointed to by ptr.
  • ptr->age accesses the age member of the object pointed to by ptr.

Summary

The -> operator in C and C++ is used to access members of a structure or class through a pointer. It combines the dereferencing of the pointer and accessing the member into one operation. This operator is essential when working with pointers to structures or classes, allowing for more concise and readable code.

For a deeper understanding of C++ pointers and more advanced topics, consider exploring courses like Grokking the Coding Interview on DesignGurus.io. This course covers essential coding and algorithm techniques that can help you master programming concepts and succeed in technical interviews.

TAGS
Coding Interview
CONTRIBUTOR
Design Gurus Team
-

GET YOUR FREE

Coding Questions Catalog

Design Gurus Newsletter - Latest from our Blog
Boost your coding skills with our essential coding questions catalog.
Take a step towards a better tech career now!
Explore Answers
What is the biggest challenge in DevOps?
What is IBM coding assessment?
Selecting appropriate data formats for given input characteristics
Does OpenAI ask for LeetCode?
How to manage imposter syndrome during interviews?
How to prepare for a technical interview with no experience?
Related Courses
Grokking the Coding Interview: Patterns for Coding Questions course cover
Grokking the Coding Interview: Patterns for Coding Questions
The 24 essential patterns behind every coding interview question. Available in Java, Python, JavaScript, C++, C#, and Go. The most comprehensive coding interview course with 543 lessons. A smarter alternative to grinding LeetCode.
4.6
Discounted price for Your Region

$197

Grokking Modern AI Fundamentals course cover
Grokking Modern AI Fundamentals
Master the fundamentals of AI today to lead the tech revolution of tomorrow.
3.9
Discounted price for Your Region

$72

Grokking Data Structures & Algorithms for Coding Interviews course cover
Grokking Data Structures & Algorithms for Coding Interviews
Unlock Coding Interview Success: Dive Deep into Data Structures and Algorithms.
4
Discounted price for Your Region

$78

Design Gurus logo
One-Stop Portal For Tech Interviews.
Copyright © 2026 Design Gurus, LLC. All rights reserved.