Skip to content

Instantly share code, notes, and snippets.

@misakar
Last active August 29, 2015 14:22
Show Gist options
  • Save misakar/b7b5449533706444f8d9 to your computer and use it in GitHub Desktop.
Save misakar/b7b5449533706444f8d9 to your computer and use it in GitHub Desktop.
C语言函数问题
/***
* C语言函数的传值调用与传址调用
*/
# include<stdio.h>
# include<stdlib.h>
# include<conio.h>
int test1(int a,int b);
int test2(int *pa,int *pb);
int test3(int &a,int &b);
int main()
{
int a = 1;
int b = 2;
int *pa = &a;
int *pb = &b;
//test1
printf("a=%d,b=%d\n",test1(a,b));
//test2
printf("a=%d,b=%d\n",test2(pa,pb));
//test3
printf("a=%d,b=%d\n",test3(&a,&b));
return 0;
}
int test1(int a,int b)
{
/*传值调用,仅仅改变了局部变量,全局变量不受影响*/
int temp;
temp = a;
a = b;
b = temp;
return a,b;
}
int test2(int *pa,int *pb)
{
/*传值调用,仅仅改变了局部变量的地址,实参不受影响*/
int *temp;
temp = pa;
pa = pb;
pb = temp;
return *pa,*pb;
}
int test3(int &a,int &b)
{
/*传址调用,直接操纵全局变量的地址*/
int temp;
temp = a;
a = b;
b = temp;
return a,b;
}
@misakar
Copy link
Author

misakar commented May 28, 2015

问题已经解决

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment