fork download
  1. #include <iostream>
  2.  
  3. void modify_pointer_byvalue(const char* p)
  4. {
  5. p = "by value";
  6. }
  7.  
  8. void modify_pointer_byreference(const char*& p)
  9. {
  10. p = "by reference";
  11. }
  12.  
  13. void modify_pointer_byindirection(const char** p)
  14. {
  15. *p = "by indirection";
  16. }
  17.  
  18. int main()
  19. {
  20. const char* ptr = "original";
  21. std::cout << "Before modification: \"" << ptr << "\"\n";
  22.  
  23. modify_pointer_byvalue(ptr);
  24. std::cout << "After value: \"" << ptr << "\"\n";
  25.  
  26. modify_pointer_byreference(ptr);
  27. std::cout << "After reference: \"" << ptr << "\"\n";
  28.  
  29. modify_pointer_byindirection(&ptr);
  30. std::cout << "After indirection: \"" << ptr << "\"\n";
  31. }
Success #stdin #stdout 0s 3340KB
stdin
Standard input is empty
stdout
Before modification: "original"
After value: "original"
After reference: "by reference"
After indirection: "by indirection"