user-0 提问: 这个程序那里错了

回复:
user: user-1
content: swap3 你给了两个指针给它，x和y记录的是2个地址，这个是不可能交换的

user: user-2
content: #include<stdio.h>  void swap1(int x, int y) { 	int tmp; 	tmp = x; 	x   = y; 	y   = tmp; }  void swap2(int *x, int *y) { 	int tmp; 	     tmp = *x; 	*x  = *y;   	*y  = tmp; }  void swap3(int *x, int *y) { 	int *tmp; 	     tmp = x;     x   = y;     y   = tmp; }  int main(void) { 	int a, b; 	int *pa, *pb; 	 	pa = &a; 	pb = &b;  	a = 10, b = 20; 	swap1(a, b); 	printf("a=%d,b=%d,*pa=%d,*pb=%d\n", a, b, *pa, *pb); 	 	a = 10, b = 20; 	swap2(pa, pb); 	printf("a=%d,b=%d,*pa=%d,*pb=%d\n", a, b, *pa, *pb); 	 	a = 10, b = 20; 	swap3(pa, pb); 	printf("a=%d,b=%d,*pa=%d,*pb=%d\n", a, b, *pa, *pb); 	 	return 0; }

user: user-2
content: #include<stdio.h>  void swap1(int x,int y) { 	int tmp; 	tmp = x; 	x   = y; 	y   = tmp; }  void swap2(int *x,int *y) { 	int tmp; 	     tmp = *x; 	*x  = *y;   	*y  = tmp; }  void swap3(int *x,int *y) { 	int *tmp; 	     tmp = x;     x   = y;     y   = tmp; }  int main(void) { 	int a, b; 	int *pa, *pb; 	 	pa = &a; 	pb = &b;  	a = 10, b = 20; 	swap1(a, b); 	printf("a=%d,b=%d,*pa=%d,*pb=%d\n", a, b, *pa, *pb); 	 	a = 10,b = 20; 	swap2(pa, pb); 	printf("a=%d,b=%d,*pa=%d,*pb=%d\n", a, b, *pa, *pb); 	 	a = 10, b = 20; 	swap3(pa, pb); 	printf("a=%d,b=%d,*pa=%d,*pb=%d\n", a, b, *pa, *pb); 	 	return 0; } 代码需要规范一下，并且代码中不要出现中文括号和分号！

user: user-3
content: 引用 7 楼 zhao4zhong1 的回复:仅供参考#include <stdio.h> #define SWAP(a,b) do ((&(a))!=(&(b)))?((a)^=(b)^=(a)^=(b)):((a)=(a)); while (0) char   *p1="1" ,*p2="2" ; char    c1=1   , c2=2   ; short   s1=1   , s2=2   ; int     i1=1   , i2=2   ; __int64 I1=1i64, I2=2i64; float   f1=1.0f, f2=2.0f; double  d1=1.0 , d2=2.0 ; void main() {     SWAP((int)p1,(int)p2);                printf("char *     %5s,   %5s\n",p1,p2);     SWAP(c1,c2);                          printf("char       %5d,   %5d\n",c1,c2);     SWAP(s1,s2);                          printf("short      %5d,   %5d\n",s1,s2);     SWAP(i1,i2);                          printf("int        %5d,   %5d\n",i1,i2);     SWAP(I1,I2);                          printf("__int64 %5I64d,%5I64d\n",I1,I2);     SWAP(*(int     *)&f1,*(int     *)&f2);printf("float      %5g,   %5g\n",f1,f2);     SWAP(*(__int64 *)&d1,*(__int64 *)&d2);printf("double    %5lg,  %5lg\n",d1,d2);      SWAP(c1,c1);     printf("%d\n",c1); } //char *         2,       1 //char           2,       1 //short          2,       1 //int            2,       1 //__int64     2,    1 //float          2,       1 //double        2,      1 //2 赵老师这编程风格，我怎么感觉我从来没学过C一样呢？

user: user-4
content: 仅供参考#include <stdio.h> #define SWAP(a,b) do ((&(a))!=(&(b)))?((a)^=(b)^=(a)^=(b)):((a)=(a)); while (0) char   *p1="1" ,*p2="2" ; char    c1=1   , c2=2   ; short   s1=1   , s2=2   ; int     i1=1   , i2=2   ; __int64 I1=1i64, I2=2i64; float   f1=1.0f, f2=2.0f; double  d1=1.0 , d2=2.0 ; void main() {     SWAP((int)p1,(int)p2);                printf("char *     %5s,   %5s\n",p1,p2);     SWAP(c1,c2);                          printf("char       %5d,   %5d\n",c1,c2);     SWAP(s1,s2);                          printf("short      %5d,   %5d\n",s1,s2);     SWAP(i1,i2);                          printf("int        %5d,   %5d\n",i1,i2);     SWAP(I1,I2);                          printf("__int64 %5I64d,%5I64d\n",I1,I2);     SWAP(*(int     *)&f1,*(int     *)&f2);printf("float      %5g,   %5g\n",f1,f2);     SWAP(*(__int64 *)&d1,*(__int64 *)&d2);printf("double    %5lg,  %5lg\n",d1,d2);      SWAP(c1,c1);     printf("%d\n",c1); } //char *         2,       1 //char           2,       1 //short          2,       1 //int            2,       1 //__int64     2,    1 //float          2,       1 //double        2,      1 //2

user: user-5

user: user-6
content: 括号和分号变成全角的了

user: user-7
content: 不知道你要问的是什么 只有swap2能交换成功  是不是你的void swap2（int *x,int *y）这个括号“（）”错了？改成英文的"()"  还有swap3的那个括号

user: user-8
content: swap1交换不了，swap2可以交换，swap3使用未初始化的指针错误

user: user-9
content: 1、请把你的中文括号“（）”改为英文的“()” 2、swap2可以把实参内容交换；swap1 和swap3都是交换形参，对实参没影响，没什么用途。

user: user-10
content: 错了？都能编译通过运行正常 lz要把疑问说出来 swap1交换的是整数副本 swap2交换的是指针指向的内存 swap3交换的是指针副本