What does “->” mean in C/C++ programming?

What does “->” mean in C/C++ programming?

When you wish to access a member of a structure via a pointer, you have to de-reference the pointer first.

IE if a is a pointer to a structure in which b is a member then you access b with (*a).b

This is such a common occurrence in C that a shorthand exists: a->b

Certainly it’s true that

p->a

is equivalent to

(*p).a

But I’d add some more detail. First of all, p is always a pointer (or pointer-like entity, such as an iterator) in this context. In C, a pointer might very often point to an instance of a structure; in C++, a pointer might often point to an object.

What does “->” mean in C/C++ programming?

+It is so common to use pointers to structures and objects, the “arrow” operator (->) was added to the language to save a few keystrokes for this pointer-dereference-and-access operation.

And yet this saves only a few keystrokes, perhaps not enough to justify its inclusion in the list of C/C++ operators. The real reason for its inclusion its that in certain programs, it’s very common to have linked-list and tree structures, such as this:

struct Node {   int val;   Node *next;} *root;

Given such a definition, it’s common to reference the next node in the list, and the next after that, etc., so that you might have code like this:

root->next->next->val = 100;

This means: access the third item in the linked list, and assign the value 100. This code is definitely more elegant and readable than the following, quite apart from a number of keystrokes that are saved.

(*(*(*root).next).next).val = 100;

What does “->” mean in C/C++ programming?

“->” is referred to as an arrow operator and is used to dereferencing a pointer, so the syntax is the same as (*ptr). Dereferencing a pointer means to get the value of that address, so for example:

struct Person{

string name;

int age;

};

Person *Bob = new Person;

Bob -> age = 21;

Notice here how we use the arrow operator to access Bob’s age and assign it to the value of 21? Because dereferencing a pointer means to get the value or access the value.

So in this case, we are accessing the value of age, which is currently uninitialized (so in that memory address, it holds nothing, but space is allocated), and then assigning an integer of 4 bytes.

What does "->" mean in C/C++ programming
What does “->” mean in C/C++ programming?

It is member access operator. To put it simply “->” allows access to member variables of a structure, class or union data type.

C++ allows user to allocate memory to a variable either at the compile time or dynamically at the run time. When dynamic memory is allocated to the structure, class or union data type (using new, malloc or other methods) then the access to its member variables is performed using “->” operator.

When the memory is allocated at compile time (local / static declaration) then the member variables are access using dot operator.

In case of classes only public members or methods can be accessed using the “->” or “.” operator.

To illustrate this, see following example –

  1. #include <iostream>
  2. using namespace std;
  3. class Rectangle {
  4. int width, height;
  5. public:
  6. int example;
  7. void set_values (int,int);
  8. int area() {return width*height;}
  9. };
  10. void Rectangle::set_values (int x, int y) {
  11. width = x;
  12. height = y;
  13. }
  14. int main () {
  15. Rectangle *rectDynamic = new Rectangle();
  16. rectDynamic ->set_values (3,4);
  17. cout << “area: ” << rectDynamic ->area();
  18. rectDynamic->example = 0;
  19. delete rectDynamic;
  20. Rectangle rectStack;
  21. rectStack.set_values (3,4);
  22. cout << “area: ” << rectStack.area();
  23. rectStack.example = 0;
  24. }

Previous answers are right, but I’ll add another case when c++ uses -> token:

template <typename T, typename T1> auto foo(T a, T1 b) -> decltype(a + b)

C++ since 2011 standard uses this as an alternative, cleaner way to declare the return type of function. First, it’s declares function ‘foo’ with automatically deduced type, by keyword ‘auto’, after parameter list you’ll see ‘->’ followed by actual supposed type.

In this case, it’s a type of result for operation (a+b). As you see, because a and b are function’s parameters, there wouldn’t be any other way to declare that type correctly but at the end of the function’s header.

What does “!” mean in Java?

What is Clickfunnels?

Jessica Morris

Hello friends, I am Jessica Morris an education blogger from New york.

You may also like...

Leave a Reply