/* Filename: pointers.cpp Author: Br. David Carlson Date: July 29, 1998 Last Revised: December 1, 2001 This program places a few values into integer and pointer variables and then prints their values. It illustrates simple uses of pointers. Tested with: Microsoft Visual C++ 6.0 Microsoft Visual C++ .NET g++ under Linux */ #include using namespace std; typedef int * IntPtrType; int main(void) { IntPtrType NumPtr; int AgeOfMary, AgeOfBill; AgeOfMary = 19; AgeOfBill = 21; NumPtr = &AgeOfMary; // get address of the AgeOfMary variable cout << "After setting NumPtr to point to AgeOfMary, NumPtr is " << NumPtr << endl; cout << "NumPtr points to " << *NumPtr << endl; cout << "AgeOfMary is " << AgeOfMary << endl << "AgeOfBill is " << AgeOfBill << endl << endl; NumPtr = &AgeOfBill; cout << "After moving NumPtr to point to AgeOfBill, NumPtr is " << NumPtr << endl; cout << "NumPtr points to " << *NumPtr << endl; cout << "AgeOfMary is " << AgeOfMary << endl << "AgeOfBill is " << AgeOfBill << endl << endl; *NumPtr = 22; cout << "After changing *NumPtr, NumPtr is " << NumPtr << endl; cout << "NumPtr points to " << *NumPtr << endl; cout << "AgeOfMary is " << AgeOfMary << endl << "AgeOfBill is " << AgeOfBill << endl << endl; NumPtr = NULL; cout << "After changing NumPtr to NULL, NumPtr is " << NumPtr << endl; cout << "We cannot print *NumPtr as NumPtr is NULL" << endl; cout << "AgeOfMary is " << AgeOfMary << endl << "AgeOfBill is " << AgeOfBill << endl << endl; return 0; }