Swapping values using pointers

Posted by xbonez on Stack Overflow See other posts from Stack Overflow or by xbonez
Published on 2010-05-14T02:26:39Z Indexed on 2010/05/14 2:34 UTC
Read the original article Hit count: 255

Filed under:

I have this code fragment

int i = 5;
int k = 7;
int * iPtr;
int * jPtr;
int * kPtr;

iPtr = &i;
kPtr = &k;

I am required to swap i and k using the pointers. This is how I'm doing it:

*jPtr = *kPtr ;
*kPtr = *iPtr ;
*iPtr = *jPtr ;

Is this the best way to do it, or is there a better way?

© Stack Overflow or respective owner

Related posts about c++