< previous page page_973 next page >

Page 973
expression *patientPtr denotes a struct variable of type PatientRec. Furthermore, the expressions (*patientPtr).idNum, (*patientPtr).height, and (*patientPtr).weight denote the idNum, height, and weight members of *patientPtr. Notice how the accessing expression is built.
patientPtr
A pointer variable of type pointer to PatientRec.
*patientPtr
A struct variable of type PatientRed.
(*patientPtr).weight
The weight member of a struct variable of type PatientRec.

The expression (*patientPtr).weight is a mixture of pointer dereferencing and struct member selection. The parentheses are necessary because the dot operator has higher precedence than the dereference operator (see Appendix B for C++ operator precedence). Without the parentheses, the expression *patientPtr.weight would be interpreted wrongly as *(patientPtr.weight).
When a pointer points to a struct (or a class or a union) variable, enclosing the pointer dereference within parentheses can become tedious. In addition to the dot operator, C++ provides another member selection operator: ->. This arrow operator consists of two consecutive symbols: a hyphen and a greater-than symbol. By definition,
PointerExpression -> MemberName
is equivalent to
(*PointerExpression).MemberName
Therefore, we can write (*patientPtr).weight as patientPtr->weight. The general guideline for choosing between the two member selection operators (dot and arrow) is the following:
Use the dot operator if the first operand denotes a struct, class, or union variable; use the arrow operator if the first operand denotes a pointer to a struct, class, or union variable.
If we want to increment and print the TimeType class object pointed to by timePtr, we could use either the statements
(*timePtr).Increment();
(*timePtr).Write();
or the statements
timePtr->Increment();
timePtr->Write();
And if we had declared an array of pointers

 
< previous page page_973 next page >