Missing explanation about -> operator


#1

As a tip, I think that the chapter 12 text omitted an explanation about the motivation of the -> operator.

Until the unexplained use of the -> operator, all structure members was accessed with the . operator in the book. Why use the -> operator?

Follow the reason illustrated with book code (I have omitted the typedef definition):

Note that p is a pointer to a structure of type struct Person. Once p points to a Person structure, *p is the structure, and b.heightInMeters[/b] and b.weightInKilos[/b] are the members. The parentheses are necessary in b.heightInMeters[/b] because the precedence of the structure member operator . is higher than . The expression *p.heightInMeters is equivalent to [b](p.heightInMeters)[/b], which is illegal in this context because heightInMeters is not a pointer.

An alternative notation is provided as a shorthand because pointers to structures are so frequently used. If p is a pointer to a structure, then p->member is a shortcut to b.member[/b].

As a suggestion, I think that this explanation should be in the next edition of the book.


#2

Thanks!! This really clears things up.


#3

[quote=“rogeriom”]As a tip, I think that the chapter 12 text omitted an explanation about the motivation of the -> operator.

Until the unexplained use of the -> operator, all structure members was accessed with the . operator in the book. Why use the -> operator?

Follow the reason illustrated with book code (I have omitted the typedef definition):

Note that p is a pointer to a structure of type struct Person. Once p points to a Person structure, *p is the structure, and b.heightInMeters[/b] and b.weightInKilos[/b] are the members. The parentheses are necessary in b.heightInMeters[/b] because the precedence of the structure member operator . is higher than . The expression *p.heightInMeters is equivalent to [b](p.heightInMeters)[/b], which is illegal in this context because heightInMeters is not a pointer.

An alternative notation is provided as a shorthand because pointers to structures are so frequently used. If p is a pointer to a structure, then p->member is a shortcut to b.member[/b].

As a suggestion, I think that this explanation should be in the next edition of the book.[/quote]

If I could, I would vote this post up. This really made it clear for me. I understood that the notation changed because we were using a pointer, but I didn’t understand why. Thanks!