fork download
#include <iostream>

void swap(int *&a, int *&b) 
{
    int *tmp = a;
    a = b;
    b = tmp;
}

int main()
{
    int i=1, j=2 ;
 
    int* p_i = &i ;
    int* p_j = &j ;
 
 
    std::cout << "Before swap, i=" << i << " j=" << j << '\n';
    std::cout << "Before swap, p_i=" << p_i << " p_j=" << p_j << "\n\n" ;
    swap(p_i, p_j) ;
    std::cout << "After swap, i=" << i << " j=" << j << '\n' ;
    std::cout << "After swap, p_i=" << p_i << " p_j=" << p_j << '\n' ;
}
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