2011年10月11日

C下不用中间变量交换两个变量值

 1 #include<stdio.h>
 2 
 3 void inplace_swap(int *, int *);
 4 void main(){
 5     int a = 10;
 6     int b = 20;
 7     
 8     inplace_swap(&a, &b);
 9     system("pause");
10 }
11 
12 void inplace_swap(int *x, int *y){
13     *x = *x ^ *y;
14     printf("%d, %d\n", *x, *y);
15     
16     *y = *x ^ *y;
17     printf("%d, %d\n", *x, *y);
18     
19     *x = *x ^ *y;    
20     printf("%d, %d\n", *x, *y);
21 }

posted @ 2011-10-11 13:19 灰色客栈 阅读(291) | 评论 (0) | 编辑 收藏

<2011年10月>
日一二三四五六
2526272829301
2345678
9101112131415
16171819202122
23242526272829
303112345

导航

统计

常用链接

留言簿

随笔档案

文章档案

搜索

最新评论

阅读排行榜

评论排行榜