< previous page page_974 next page >

Page 974
PatientRec* patPtrArray[20];
and initialized the array elements, then we could access the idNum member of the fourth patient as follows:
patPtrArray[3]->idNum
Pointed-to variables can be used in the same way as any other variable. The following statements are all valid:
*intPtr = 250;
*colorPtr = RED;
patientPtr->idNum = 3245;
patientPtr->height = 64;
patientPtr->weight = 114;
patPtrArray[3]->idNum = 6356;
patPtrArray[3]->height = 73;
patPtrArray[3]->weight = 185;
Figure 17-4 shows the results of these assignments.
At this point, you may be wondering why we should use pointers at all. Instead of making intPtr point to alpha and storing 250 into *intPtr, why not just store 250 directly into alpha? The truth is that there is no good reason to program this way; on the contrary, the examples we have shown would make a program more roundabout and confusing. The major use of pointers in C++ is to manipulate dynamic variablesvariables that come into existence at execution time only as they are needed. Later in the chapter, we show how to use pointers to create dynamic variables. In the meantime, let's continue with some of the basic aspects of pointers themselves.
Pointer Expressions
You learned in the early chapters that an arithmetic expression is made up of variables, constants, operator symbols, and parentheses. Similarly, pointer expressions are composed of pointer variables, pointer constants, certain allowable operators, and parentheses. We have already discussed pointer variablesvariables that hold addresses of other variables. Let's look now at pointer constants.
In C++, there is only one literal pointer constant: the value 0. The pointer constant 0, called the null pointer, points to absolutely nothing. The statement
intPtr = 0;

 
< previous page page_974 next page >