#include // A pointer stores the address of another object. // // Given // // int m{ 42 }; // int *p_m{ &m }; // // the expression p_m is the address of m, while // the expression *p_m is the value stored at that // address. // // Consequently: // // m = 10; // // and // // *p_m = 10; // // both change the value of m. // // However, assigning another address to p_m changes // which integer p_m points to. It does not change // either integer. // // In this exercise, you will perform three swaps: // // 1. Swap the values of two integer variables. // // 2. Swap the integer values stored at the addresses // held by two pointers. // // 3. Swap the addresses stored in the two pointers. // // Before running your program, predict the values // of m, n, *p_m, and *p_n after each operation. // // You may not use std::swap. // Function declarations int main(); // Function definitions int main() { int m{ 42 }; int n{ 91 }; int *p_m{ &m }; int *p_n{ &n }; // Initially: // // m = 42 *p_m = 42 // n = 91 *p_n = 91 // // Also, p_m == &m and p_n == &n. // 1. Swap the integers m and n. // Predict the values after the first swap: // // m = ?? *p_m = ?? // n = ?? *p_n = ?? // 2. Swap the integer values stored at the // addresses held by p_m and p_n. // // Do not change the addresses in the pointers. // Predict the values after the second swap: // // m = ?? *p_m = ?? // n = ?? *p_n = ?? // 3. Swap the addresses stored in the pointer // variables p_m and p_n. // // Do not change the values of m or n. // Predict the final values before running // your program. std::cout << " m = " << m << " (= ??)" << std::endl; std::cout << "*p_m = " << *p_m << " (= ??)" << std::endl; std::cout << " n = " << n << " (= ??)" << std::endl; std::cout << "*p_n = " << *p_n << " (= ??)" << std::endl; // Which variable does each pointer now // point to? Verify your prediction. std::cout << std::boolalpha; std::cout << "p_m == &m: " << (p_m == &m) << std::endl; std::cout << "p_m == &n: " << (p_m == &n) << std::endl; std::cout << "p_n == &m: " << (p_n == &m) << std::endl; std::cout << "p_n == &n: " << (p_n == &n) << std::endl; return 0; } // Hint: // // If you are swapping two integer values, // your temporary variable should have type int: // // int tmp{ m }; // // If you are swapping two addresses, your // temporary variable should have type int *: // // int *p_tmp{ p_m }; // // Remember: // // p_m // // is an address, whereas // // *p_m // // is the integer stored at that address. // Another hint: // // To change the integer stored at the address // held by a pointer, use: // // *p_m = another_integer_value; // // To change which address a pointer stores, use: // // p_m = another_address; // // In particular, the expression // // *p_m = *p_n; // // copies an integer value. It does not change // either pointer. // // By contrast, // // p_m = p_n; // // copies an address. Both pointers then point // to the same integer. // Here is one solution: /* int main() { int m{ 42 }; int n{ 91 }; int *p_m{ &m }; int *p_n{ &n }; // 1. Swap the integers m and n. int tmp{ m }; m = n; n = tmp; // Now: // // m = 91 *p_m = 91 // n = 42 *p_n = 42 // 2. Swap the values stored at the addresses // held by p_m and p_n. tmp = *p_m; *p_m = *p_n; *p_n = tmp; // Now: // // m = 42 *p_m = 42 // n = 91 *p_n = 91 // 3. Swap the addresses stored in the pointers. int *p_tmp{ p_m }; p_m = p_n; p_n = p_tmp; // The integer values have not changed, but // the pointers now point to different integers. // // p_m == &n // p_n == &m std::cout << " m = " << m << " (= 42)" << std::endl; std::cout << "*p_m = " << *p_m << " (= 91)" << std::endl; std::cout << " n = " << n << " (= 91)" << std::endl; std::cout << "*p_n = " << *p_n << " (= 42)" << std::endl; std::cout << std::boolalpha; std::cout << "p_m == &m: " << (p_m == &m) << std::endl; std::cout << "p_m == &n: " << (p_m == &n) << std::endl; std::cout << "p_n == &m: " << (p_n == &m) << std::endl; std::cout << "p_n == &n: " << (p_n == &n) << std::endl; return 0; } */ // Alternative: // // If the declaration // // int *p_tmp{ p_m }; // // is confusing, separate the declaration // from the assignment: // // int *p_tmp{ nullptr }; // // p_tmp = p_m; // p_m = p_n; // p_n = p_tmp; // // A pointer can be assigned another pointer's // value, provided their pointer types are // compatible. In this example, both store // addresses of integers.