Skip to content

Instantly share code, notes, and snippets.

@misakar
Created May 26, 2015 14:55
Show Gist options
  • Save misakar/48b43f2669940972143b to your computer and use it in GitHub Desktop.
Save misakar/48b43f2669940972143b to your computer and use it in GitHub Desktop.
/*交换a,b的值*/
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
//函数的声明<br/>
int change_1(int a,int b);
int change_2(int *pa,int *pb);
int change_3(int &a,int &b);
int main()
{
int a,b;
int *pa,*pb;
a = 1;
b = 2;
pa = &a;
pb = &b;
//test1
change_1(a,b);
printf("%d%d\n",a,b);
//test2
change_2(pa,pb);
printf("%d%d\n",a,b);
//test3
change_3(a,b);
printf("%d%d\n",a,b);
return 0;
}
int change_1(int a,int b){
/*值传递*/
int temp;
temp = a;
a = b;
b = temp;
return 0;
}
int change_2(int *pa,int *pb){
/*指针传递(值传递的另一种方式)*/
int *temp;
temp = pa;
pa = pb;
pb = temp;
return 0;
}
int change_3(int &a,int &b){
/*址传递*/
int temp;
temp = &a;
&a = &b;
&b = temp;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment