Real difference between point operator (.) and arrow operator (->) in C?

Asked

Viewed 17,517 times

16

What is the real difference between the two operators.

I know the operator (->) is used when the variable is a pointer, and that it is equivalent to (*ptr).membro. Well, if I declare a pointer of the type of a structure, I must use the arrow operator. If I declare an ordinary variable of the structure type, I should use the point operator. This I know.

What I would really like to know is: what is the advantage of one in relation to the other? When should I use one in view of the other? I haven’t seen the advantage of having two operators who apparently do the same thing.

  • 2

    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 .).

  • p->v would be a shortcut to (*p). v, if there is any advantage, I believe it is in organization.

2 answers

14


Both are equivalent, there is an advantage in using one or the other, usually the copier has something called "syntactic sugar", which is actually a facilitation to write the code, this is very visible in java where you can exchange a + for a flame a new Stringbuilder().

If there is any Advantage, it would be in the organization/readability of the code !

(*(*(*a).b).c).d // não intuitivo

a->b->c->d // intuitivo

I looked for a reference to verify what I am going to say now but I did not find, in some copiers, there is the pre-copying substitution, the copier before performing c-Object replaces all occurrences of -> by (*p). v, I know this is documented in the OCA books of java, it may be that the same occurs in c.

  • So given a type struct a guy *a of struct and a guy a also of struct will have the same effect?

13

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.

  • 1

    I liked the answer you explained about C++ and "how C was planned".

Browser other questions tagged

You are not signed in. Login or sign up in order to post.