尼玛,今天一下就被颠覆三观了,原来我一直把指针和引用搞混了,次奥!!!!!!!
#include<stdio.h>
void pt(int * pta,int * ptb)
{
int *ptc;
ptc=pta;pta=ptb;ptb=ptc;
}
void ref(int &ra,int &rb)
{
int rc;
rc=ra;ra=rb;rb=rc;
}
void main()
{
int a=3;int b=4;
int *pa=&a;int *pb=&b;
pt(pa,pb);
printf("zhizhen: a=%d,b=%d\n",a,b);
ref(a,b);
printf("yinyong: a=%d,b=%d\n",a,b);
}
输出:
zhizhen: a=3,b=4
yinyong:a=4,b=3
再加上一段代码,以便能够更深入的理解这个东西
void tt(int *a,int *b){
int c;
c=*a,*a=*b,*b=c;
}
int main(){
int a=5,b=6;
int *pta=&a,*ptb=&b;
printf("%d %d\n",a,b);
tt(pta,ptb);
printf("%d %d",a,b);
while(1);
}
输出:
5 6
6 5