#include #include using std::cout; using std::endl; using std::string; /* Note: Generally, having global variables is a bad design. They're used here to be visible in printValues (without have to pass them in each time) */ int number = -1; int* intPtr1; int* intPtr2; void printValues(string header){ cout << endl; cout << header << endl; cout << "---------------------------------\n"; cout << "&number: " << &number << ", number: " << number << endl; cout << "&intPtr1: " << &intPtr1 << ", intPtr1: " << intPtr1 << ", *intPtr1: "; if( intPtr1 != nullptr){ cout << *intPtr1; }else{ cout << "nullptr"; } cout << endl; cout << "&intPtr2: " << &intPtr2 << ", intPtr2: " << intPtr2 << ", *intPtr2: "; if( intPtr2 != nullptr){ cout << *intPtr2; }else{ cout << "nullptr"; } cout << endl; } int main(){ printValues("Initial"); printValues("1) After making intPtr1 point to number\n"); printValues("2) After assigning 3 to the variable that intPtr1 is pointing to (using intPtr1)\n"); printValues("3) After making intPtr1 point to a newly allocated int\n"); printValues("4) After assigning that new int to 7\n"); printValues("5) After copying the address stored in intPtr1 to intPtr2\n"); printValues("6) After making intPtr2 point to a newly allocated int\n"); printValues("7) After assigning that new int to 1\n"); printValues("8) After freeing up the allocated memory and set both pointers to NULL\n"); return 0; }