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 之類我就不談了)