1 #include<stdio.h> 2 void swap1(int a,int b); //利用异或交换变量的值,值传递 3 void swap2(int* a,int* b); //利用异或交换变量的值,地址传递,这里用指针* 4 5 6 void swap1(int a,int b) /*深拷贝值传递*/ 7 { 8 a = a ^ b; 9 b = a ^ b; 10 a = a ^ b; 11 } 12 13 void swap2(int* a,int* b) /*浅拷贝地址传递*/ 14 { 15 *a = *a ^ *b; 16 *b = *a ^ *b; 17 *a = *a ^ *b; 18 } 19 20 int main(void) 21 { 22 int a1 = 5,b1 = 10; 23 printf("swap1交换前:地址:a1 = %p,b1 = %p..值:a1 = %d,b1 = %d ",a1,b1,a1,b1); 24 swap1(a1,b1); 25 printf("swap1交换后:地址:a1 = %p,b1 = %p..值:a1 = %d,b1 = %d ",a1,b1,a1,b1); 26 int a2 = 5,b2 = 10; 27 printf("swap2交换前:地址:a2 = %p,b2 = %p..值:a2 = %d,b2 = %d ",a2,b2,a2,b2); 28 swap2(&a2,&b2); 29 printf("swap2交换后:地址:a2 = %p,b2 = %p..值:a2 = %d,b2 = %d ",a2,b2,a2,b2); 30 return 0; 31 }