Last active
August 29, 2015 14:22
-
-
Save misakar/b7b5449533706444f8d9 to your computer and use it in GitHub Desktop.
C语言函数问题
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*** | |
* 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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
问题已经解决