fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. void passPointer(int *x);
  5.  
  6. int main()
  7. {
  8. int value = 10;
  9. int* vPtr = &value;
  10.  
  11. std::cout << "value before function call: " << value << '\n';
  12. std::cout << "Address contained by vPtr before function call: " << vPtr << '\n';
  13.  
  14. passPointer(vPtr);
  15.  
  16. std::cout << "value after function call: " << value << '\n';
  17. std::cout << "Address contained by vPtr after function call: " << vPtr << '\n';
  18. }
  19.  
  20. void passPointer(int *x){
  21. int local_variable;
  22.  
  23. std::cout << "Address of local_variable: " << &local_variable << '\n';
  24.  
  25. *x = 20; // derefence allows one to get a reference to the object pointed to.
  26. x = &local_variable; // not dereferencing means we're modifying a copy of the pointer.
  27. }
Success #stdin #stdout 0s 3096KB
stdin
Standard input is empty
stdout
value before function call: 10
Address contained by vPtr before function call: 0xbfd1404c
Address of local_variable: 0xbfd1401c
value after function call: 20
Address contained by vPtr after function call: 0xbfd1404c