通过传地址,传引用交换两个数的值
2022-11-17 20:21 钟铧若岩 阅读(24) 评论(0) 编辑 收藏 举报
1 #include <cstdio> 2 #include <iostream> 3 using namespace std; 4 5 void swapxy(char *a ,char *b) 6 { 7 int c = *a; 8 *a = *b; 9 *b = c; 10 } 11 void swapxy(char &a,char &b) 12 { 13 int c = a; 14 a = b; 15 b = c; 16 } 17 18 19 int main() 20 { 21 char a = 'f'; 22 char b = 'g'; 23 24 swapxy(&a,&b); 25 printf("a = %c, b = %c\n",a,b); 26 27 char &x = a; 28 char &y = b; 29 30 swapxy(x,y); 31 printf("a = %c, b = %c\n",a,b); 32 33 return 0; 34 }