int *countPtr, count;
declares the variable countPtr to be of type int * (i.e., a pointer to an int value) and is read, “countPtr is a pointer to int” or “countPtr points to an object of type int.” Also, variable count in the preceding declaration is declared to be an int, not a pointer to an int. The * in the declaration applies only to countPtr. Each variable being declared as a pointer must be preceded by an asterisk (*). For example, the declaration
double *xPtr, *yPtr;
indicates that both xPtr and yPtr are pointers to double values. *Pointers should be initialized either when they are declared or in an assignment. A pointer may be initialized to 0, NULL or an address. A pointer with the value 0 or NULL points to nothing and is known as a null pointer. Symbolic constant NULL is defined in header file <iostream> (and in several other standard library header files) to represent the value 0. Initializing a pointer to NULL is equivalent to initializing a pointer to 0, but in C++, 0 is used by convention. When 0 is assigned, it is converted to a pointer of the appropriate type. The value 0 is the only integer value that can be assigned directly to a pointer variable without casting the integer to a pointer type first.
int y = 5; // declare variable y int *yPtr; // declare pointer variable yPtr
the statement
yPtr = &y; // assign address of y to yPtr
assigns the address of the variable y to pointer variable yPtr. Then variable yPtr is said to “point to” y. Now, yPtr indirectly references variable y’s value. Note that the use of the & in the preceding assignment statement is not the same as the use of the & in a reference variable declaration, which is always preceded by a data-type name.
cout << *yPtr << endl;
prints the value of variable y, namely, 5, just as the statement
cout << y << endl;
would. Using * in this manner is called dereferencing a pointer. Note that a dereferenced pointer may also be used on the left side of an assignment statement, as in
*yPtr = 9;
which would assign 9 to y. The dereferenced pointer may also be used to receive an input value as in
cin >> *yPtr;
which places the input value in y.
1 // 2 // Using the & and * operators. 3 #include <iostream> 4 using std::cout; 5 using std::endl; 6 7 int main() 8 { 9 int a; // a is an integer 10 int *aPtr; // aPtr is an int * -- pointer to an integer 11 12 a = 7; // assigned 7 to a 13 aPtr = &a; // assign the address of a to aPtr 14 15 cout << "The address of a is " << &a 16 << "\nThe value of aPtr is " << aPtr; 17 cout << "\n\nThe value of a is " << a 18 << "\nThe value of *aPtr is " << *aPtr; 19 cout << "\n\nShowing that * and & are inverses of " 20 << "each other.\n&*aPtr = " << &*aPtr 21 << "\n*&aPtr = " << *&aPtr << endl; 22 return 0; // indicates successful termination 23 } // end main
1 2 // Cube a variable using pass-by-value. 3 #include <iostream> 4 using std::cout; 5 using std::endl; 6 7 int cubeByValue( int ); // prototype 8 9 int main() 10 { 11 int number = 5; 12 13 cout << "The original value of number is " << number; 14 15 number = cubeByValue( number ); // pass number by value to cubeByValue 16 cout << "\nThe new value of number is " << number << endl; 17 return 0; // indicates successful termination 18 } // end main 19 20 // calculate and return cube of integer argument 21 int cubeByValue( int n ) 22 { 23 return n * n * n; // cube local variable n and return result 24 } // end function cubeByValue
1 // Fig. 8.7: fig08_07.cpp 2 // Cube a variable using pass-by-reference with a pointer argument. 3 #include <iostream> 4 using std::cout; 5 using std::endl; 6 7 void cubeByReference( int * ); // prototype 8 9 int main() 10 { 11 int number = 5; 12 13 cout << "The original value of number is " << number; 14 15 cubeByReference( &number ); // pass number address to cubeByReference 16 17 cout << "\nThe new value of number is " << number << endl; 18 return 0; // indicates successful termination 19 } // end main 20 21 // calculate cube of *nPtr; modifies variable number in main 22 void cubeByReference( int *nPtr ) 23 { 24 *nPtr = *nPtr * *nPtr * *nPtr; // cube *nPtr 25 } // end function cubeByReference
1 // Fig. 8.10: fig08_10.cpp 2 // Converting lowercase letters to uppercase letters 3 // using a non-constant pointer to non-constant data. 4 #include <iostream> 5 using std::cout; 6 using std::endl; 7 8 #include <cctype> // prototypes for islower and toupper 9 using std::islower; 10 using std::toupper; 11 12 void convertToUppercase( char * ); 13 14 int main() 15 { 16 char phrase[] = "characters and $32.98"; 17 18 cout << "The phrase before conversion is: " << phrase; 19 convertToUppercase( phrase ); 20 cout << "\nThe phrase after conversion is: " << phrase << endl; 21 return 0; // indicates successful termination 22 } // end main 23 24 // convert string to uppercase letters 25 void convertToUppercase( char *sPtr ) 26 { 27 while ( *sPtr != '\0' ) // loop while current character is not '\0' 28 { 29 if ( islower( *sPtr ) ) // if character is lowercase, 30 *sPtr = toupper( *sPtr ); // convert to uppercase 31 32 sPtr++; // move sPtr to next character in string 33 } // end while 34 } // end function convertToUppercase