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

Free Coding Questions Catalog
Boost your coding skills with our essential coding questions catalog. Take a step towards a better tech career now!

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
Why should I join Twitter?
What are the top system design interview questions for PayPal interview?
What is your professional weakness?
Related Courses
Image
Grokking the Coding Interview: Patterns for Coding Questions
Grokking the Coding Interview Patterns in Java, Python, JS, C++, C#, and Go. The most comprehensive course with 476 Lessons.
Image
Grokking Data Structures & Algorithms for Coding Interviews
Unlock Coding Interview Success: Dive Deep into Data Structures and Algorithms.
Image
Grokking Advanced Coding Patterns for Interviews
Master advanced coding patterns for interviews: Unlock the key to acing MAANG-level coding questions.
Image
One-Stop Portal For Tech Interviews.
Copyright © 2024 Designgurus, Inc. All rights reserved.