The arrow operator -> in C and C++ is used for accessing members (variables, methods) of a structure or class through a pointer. It’s specifically applied in scenarios involving dynamic memory allocation, linked lists, and other data structures and instances where objects are accessed through their pointers.
In the context of classes and structures in C and C++, when an object is accessed directly, its members are accessed using the dot operator (.). When an object is accessed through a pointer, the arrow operator -> is used to access its members.
The syntax can be visually understood as follows:
object.member: Using the dot operator to access a member directly
pointer->member : Using the arrow operator to access a member through a pointer
Essentially, the following relation holds true:
pointer->member == (*pointer).member
The expression (*pointer).member is a combination of dereferencing * and the dot operator ., which can be combined using the arrow operator ->.
#include <iostream>using namespace std;class Point {public:int x;int y;};int main() {Point p1 = {10, 20}; // Regular object of class PointPoint *p2 = &p1; // Pointer to p1// Accessing members of p1 using the dot operatorcout << "p1.x = " << p1.x << ", p1.y = " << p1.y << endl;// Accessing members of the object pointed by p2 using the arrow operatorcout << "p2->x = " << p2->x << ", p2->y = " << p2->y << endl;return 0;}
In the above example, p1.x and p1.y use the dot operator because p1 is a regular variable of type struct Point. In contrast, p2->x and p2->y use the arrow operator because p2 is a pointer to a struct Point.
The provided C++/C code defines a structure Sample with an integer member a, creates an object obj of that structure, uses a pointer ptr to assign the value 5 to obj.a using the arrow operator, and outputs the assigned value to the standard output.
#include<iostream>using namespace std;struct Sample {int a;};int main() {Sample obj;Sample* ptr = &obj;ptr->a = 5; // Using arrow operatorcout<< "ptr->a: " << ptr->a <<endl;return 0;}
The provided C++/C code defines a structure Sample with an integer member a, dynamically allocates memory for a Sample structure, assigns the value 5 to its a member using a pointer and the arrow operator, outputs the assigned value, and then frees the allocated memory, handling memory allocation failure with an error message.
#include<iostream>using namespace std;struct Sample {int a;};int main() {Sample* ptr = new Sample;ptr->a = 5; // Using arrow operatorcout<< "ptr->a: " << ptr->a <<endl;delete ptr; // Freeing allocated memoryreturn 0;}
The C++/C code snippet defines a class Example with an integer variable x and a member function display(), creates an object obj of Example, uses a pointer ptr to assign 10 to x and call display(), outputting the value of x using the arrow operator.
#include<iostream>using namespace std;// Define a class to hold dataclass Example {public:int x;void display() {cout << "Value of x: " << x << endl;}};int main() {Example obj;Example* ptr = &obj;ptr->x = 10; // Accessing variable using arrow operatorptr->display(); // Accessing function using arrow operatorreturn 0;}
In linked list implementations, the arrow operator is used to traverse through the nodes by accessing the next node’s address.
#include<iostream>using namespace std;struct Node {int data;Node* next;};// Function to print linked list datavoid print(Node* head) {Node* current = head;while(current != NULL) {cout << current->data << " ";current = current->next;}cout << endl;}int main() {Node* head = new Node;head->data = 1; // Assigning data to node using arrow operatorhead->next = NULL; // Ensuring the list ends hereprint(head); // Calling print to display linked list data// Free the dynamically allocated memorydelete head;return 0;}
In the linked list example, head is a pointer to a Node type, and head->data and head->next are used to access members of the structure.
The arrow operator -> is vital in managing objects and structures through pointers, especially in complex data structures. It ensures code readability by providing a clear and concise way to dereference pointers when accessing member elements, encapsulating the dereference operation (*ptr). into a straightforward ptr-> expression.
Accelerate your coding journey – Master C and C++ today with our courses:
Learn C course – Dive into C programming and master key concepts like data types, control flow, functions, pointers, and memory management. Plus, get hands-on with powerful debugging and optimization tools like GDB and gcc to refine your skills.
Learn C++ course – Jumpstart your C++ journey with interactive execution sheets and flowcharts, perfect for beginners and those aspiring to become proficient C++ developers.
Free Resources