-1

i know that -> operator can be a pointer to the direction of the variable, but why use it when you have * pointer operator, what is the difference, and can i prefer one over the other? for example:

a*b

means a is pointer to b (right? i could be mistaken forgive me because i'm a beginner)

is it equivalent to a->b ?

and a<-b meaning b is a pointer to a?

if so, is it possible to rely on -> because it's simpler?

101010
  • 41,839
  • 11
  • 94
  • 168
Ahmed Dhia
  • 11
  • 2
  • 1
    You seem to be misunderstanding what `->` means. It doesn't mean any direction, it's just shorthand for dereferencing a pointer to a structure followed by structure member access. For a pointer to a structure `a` and member `b`, the expression `a->b` is equal to `(*a).b`. ***Also*** the `->` operator used to mean something else early on in the C language (where it comes from). Read e.g. [this old answer](http://stackoverflow.com/a/13366168/440558) for more details. – Some programmer dude Feb 23 '17 at 10:06
  • http://stackoverflow.com/a/6586248/6543574 here it is explained, hope that helps :) – Lanfear Feb 23 '17 at 10:07
  • 2
    `a*b` _never_ means that `a` is a pointer to `b`. Closest possible meaning is that, as part of a declaration, it means that `b` is a pointer to an `a` (where `a` is a _type_). In expressions, `a*b` invokes multiplication. – davmac Feb 23 '17 at 10:10
  • my apologized, but how is it a regular point for a to b is written? i know that a &b possibly means that b us an alias for a (reference) – Ahmed Dhia Feb 23 '17 at 10:31
  • @AhmedDhia `int *a = &b;` – Dannyu NDos Feb 23 '17 at 10:32
  • The Definitive C++ Book Guide and List: http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – Richard Critten Feb 23 '17 at 10:45

2 Answers2

2

a*b usually means "multiply a and b". C++ has only very few lexical elements, and many of them serve many different purposes, so context is important.

a->b is member access and corresponds to (*a).b, i.e. access the b member of the value pointed to by a.

Note that both the * and the -> operators can be overloaded.

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
1

For me, it seems that you have confusion about what a pointer does, and where * belongs.
Pointer is a special type of object that points to another object, not from an object.
Pointer can be declared like this:

int *a;

Here, * specifies that a is a pointer. Pointer types are distinct type from the type of the object that they point to.
But what a points to is indeterminate. You need to assign an address to make a point to something. To do that, let's make an integer (here initialized with value 2):

int b = 2;

And let a point to b:

a = &b;

Here, the unary & operator is used to take b's address. And you assign this to a, and then a points to b.
Of course, a pointer can also be initialized. The statements above can be replaced to:

int b = 2;
int *a = &b;

Then, how do you get b's value from a? You can do that by dereferencing a:

std::cout << *a << std::endl;

This prints 2. The unary operator * is used here to dereference a. This * is different from the * used to declare a above and should not be confused.

++*a;

This increases b. b now has value 3.
You can make a point to a different object by assigning another address:

int c = 3;
a = &c;

Now a points to c instead of b.
You can copy a pointer, which will point to the same object:

int *d = a;

Now d points to c, which a also points to.

References are different from pointers:

int &e = b;

& is used here to specify e is a reference. This & is different from the & used to take an address and should not be confused. Here, e refers to b.
They refer to an object and always act as if they were that object:

++e;

This increases b. b now has value 4.
Unlike pointers, references must always refer to an object:

int &f; // error

A reference can also refer to a pointer:

int *&g = a;

g refers to a.
But a pointer cannot point to a reference:

int &*h = &e; // error
Dannyu NDos
  • 2,458
  • 13
  • 32
  • Thanks alot :) this got things clearly, one question though, how do i prevent the output from printing the memory address or location of a pointer? – Ahmed Dhia Feb 23 '17 at 12:01
  • You should just not give a memory address to output streams. Following the examples above, `std::cout << d << std::endl` will output `c`'s address. – Dannyu NDos Feb 23 '17 at 12:05