fork download
  1. #include <iostream>
  2.  
  3. void swap(int *&a, int *&b)
  4. {
  5. int *tmp = a;
  6. a = b;
  7. b = tmp;
  8. }
  9.  
  10. int main()
  11. {
  12. int i=1, j=2 ;
  13.  
  14. int* p_i = &i ;
  15. int* p_j = &j ;
  16.  
  17.  
  18. std::cout << "Before swap, i=" << i << " j=" << j << '\n';
  19. std::cout << "Before swap, p_i=" << p_i << " p_j=" << p_j << "\n\n" ;
  20. swap(p_i, p_j) ;
  21. std::cout << "After swap, i=" << i << " j=" << j << '\n' ;
  22. std::cout << "After swap, p_i=" << p_i << " p_j=" << p_j << '\n' ;
  23. }
Success #stdin #stdout 0s 3296KB
stdin
Standard input is empty
stdout
Before swap, i=1 j=2
Before swap, p_i=0xbfb8e468 p_j=0xbfb8e46c

After swap, i=1 j=2
After swap, p_i=0xbfb8e46c p_j=0xbfb8e468