C++ 的话就pass by reference:
void swap(int& a, int& b) {
int temp = a;
a = b;
b = temp;
}
int x,y; //...
swap(x,y);
C 的话一是传address:
void swap(int* a, int* b) {
int temp = *a;
*a = *b;
*b = temp;
}
int x,y; //...
swap(&x, &y);
一是写macro (不建议, 后面的随便写,会有不少问题,看看就好)
#define SWAP(a,b) { \
int temp=a; a = b; b = temp; }
int x,y; //...
SWAP(x,y);
(至于应该将会有人提到的如何不靠 temp 做
Swap 之类我就不谈了)