|
|
 |
|
 |
|
|
Pointer Type A simple data type consisting of an unbounded set of values, each of which addresses or otherwise indicates the location of a variable of a given type. Among the operations defined on pointer variables are assignment and test for equality. |
|
|
|
|
|
|
|
|
Let's begin this discussion by looking at how pointer variables are declared in C++. |
|
|
|
|
|
|
|
|
Surprisingly, the word pointer isn't used in declaring pointer variables; the symbol * is used instead. The declaration |
|
|
|
|
|
|
|
|
states that intPtr is a variable that can point to (that is, contain the address of) an int variable. Here is the syntax template for declaring pointer variables: |
|
|
|
|
|
|
|
|
The syntax template shows two forms, one for declaring a single variable and the other for declaring several variables. In the first form, the compiler does not care where the asterisk is placed. Both of the following declarations are equivalent: |
|
|
|
|
|
|
|
|
int* intPtr;
int *intPtr; |
|
|
|
|
|
|
|
|
Although C++ programmers use both styles, we prefer the first. Attaching the asterisk to the data type name instead of the variable name readily suggests that intPtr is of type pointer to int. |
|
|
|
|
|
|
|
|
According to the syntax template, if you declare several variables in one statement you must precede each variable name with an asterisk. Otherwise, only the first variable is taken to be a pointer variable. The compiler interprets the statement |
|
|
|
|
|