//pointer1.cpp #include //cout, cin, endl using namespace std; int main() { int TomsAge=35; int SuesAge=43; int * pAge=0; //a pointer to an integer. //Initialize it! //It is pointing to location 0, not the number 0. cout << "The address IN pAge: " << pAge << '\n'; cout << "The address OF pAge: " << &pAge << '\n'; pAge=&TomsAge; //store the address. //say, pAge POINTS TO TomsAge if (pAge!=0) //don't dereference null pointer! cout << "Tom's age: " << *pAge << '\n'; //*pAge, dereferencing a pointer, //the VALUE that pAge is pointing to cout << "The address IN pAge: " << pAge << '\n'; cout << " is the address OF TomsAge " << &TomsAge << '\n'; cout << "The address OF pAge: " << &pAge << " does not change." << '\n'; if (pAge!=0) { *pAge=36; //change the value in the box. //*pAge can be an l-value cout << "Tom's age: " << *pAge << '\n'; } cout << "The address IN pAge: " << pAge << '\n'; if (pAge!=0) { *pAge= SuesAge; //are we moving the pointer or changing //the data? In which box? cout << "The age pointed to is " << *pAge << '\n'; } cout << "Is it Sue's age?: "<< SuesAge << '\n'; cout << "Is it Tom's age?: " << TomsAge << '\n'; cout << "The address IN pAge: " << pAge << '\n'; //set things up again SuesAge=43; TomsAge=35; pAge=&TomsAge; cout << "Now to see pointers can move:\n"; cout << " pAge is pointing to TomsAge " << *pAge << endl; cout << " The address IN pAge is the address of TomsAge " << pAge << endl; pAge=&SuesAge; cout<<" Now pAge is pointing to SuesAge " << *pAge << endl; cout << " which is at the address " << pAge << endl; return 0; }