The real difference is that the first is a reference to the limb, you’re simply saying that you should take the limb die and the second is a dereference of the pointer contained in the limb, you’re saying you should take the pointer value on the limb. It is only syntactic sugar and the advantage is to write and read more easily. It is more obvious to see ->
and know what’s going on than interpreting (*ptr).membro
.
What does this do?
(*(*(*a).b).c).d
And this?
a->b->c->d
This is saying to take the amount pointed by d
who is a member of c
who is a member of b
who in turn is a member of a
. The first does the same thing but I won’t even risk reading it out loud. Being explicit in intention is best.
In C++ there is still the difference that the "arrow operator" can be overloaded.
C was made to be a Assembly portable and did not think much about the facilities for the programmer. If you think that normal in a program is working with end values and not with pointers, C is pretty boring. By usability the normal would be to say that you want to take the pointer by exception. But by the philosophy of C this is not ideal. She always sought to facilitate quick access and not usability. It was the 70’s.
In fact there are discussions about how the ->
is unnecessary in the language. It is really possible not to use it but had to be set at the beginning of the language. It is possible for the compiler to know how access should be done only with .
but this was realized too late. Initially the ->
wasn’t syntax sugar.
I think it’s not a matter of "advantage," it’s a (syntactic) emphasis of language to make it clear when you’re dealing with pointers to objects (and use
->
), or with the objects themselves (and uses.
).– bfavaretto
p->v would be a shortcut to (*p). v, if there is any advantage, I believe it is in organization.
– Isvaldo Fernandes