best way to do this in C ++, in my opinion, with std::iter_swap():
#include<algorithm>
int *iPtr = &i, *kPtr = &k;
std::iter_swap(iPtr, kPtr);
You might think that busting, but I would not agree if you turned <algorithm>on anyway. Of course, in the case of homework, this answer is only fulfilled if the instructor intends to introduce you to the STL. If you intend to introduce pointers to you, the best solution would be to introduce a temporary variable j:
int j;
int *iPtr = &i, *kPtr = &k, *jPtr = &j;
*jPtr = *kPtr;
*kPtr = *iPtr;
*iPtr = *jPtr;
source
share