法一:
#include<stdio.h>
int main()
{
int a = 21;
int b = 13;
int tmp = a;
a = b;
b = tmp;
return 0;
}
法二:
#include<stdio.h>
void Swap(int * px, int * py)
{
int tmp = 0;
tmp = *px;
*px = *py;
*py = tmp;
}
int main()
{
int a = 21;
int b = 13;
Swap(*a, *b);
return 0;
}
法三:不允许创建临时变量,交换两个数的内容:
#include<stdio.h>
int main()
{
int a = 21;
int b = 13;
a = a ^ b;
b = a ^ b;
a = a ^ b;
return 0;
}