C语言:三种函数方法的调用,引用调用与指针
三种函数方法的调用,
主要看change3这个函数, 此方法调用后, 会对主函数造成影响!
- #include <stdio.h>
- void change1(int a, int b)
- {
- if(a<b)
- {
- int temp;
- temp = a;
- a = b;
- b = temp;
- }
- printf("change1: a = %d, b = %d \n",a,b);
- }
- void change2(int *p1, int *p2)
- {
- if(*p1<*p2)
- {
- int *temp;
- temp = p1;
- p1 = p2;
- p2 = temp;
- }
- printf("change2: a = %d, b = %d \n",*p1,*p2);
- }
- void change3(int *p1, int *p2)
- {
- if(*p1<*p2)
- {
- int temp;
- temp = *p1;
- *p1 = *p2;
- *p2 = temp;
- }
- printf("change3: a = %d, b = %d \n",*p1, *p2);
- }
- int main()
- {
- int a, b;
- int *p1,*p2;
- printf("Input two number:\nfirst:");
- scanf("%d",&a);
- printf("second:");
- scanf("%d",&b);
- p1 = &a;
- p2 = &b;
- change1(a,b); /*方法change1中调换两个数的位置,不会对主函数有影响*/
- printf("main: a = %d, b = %d \n",a,b);
- change2(p1,p2); /*方法change2中调换两个娄的位置,不会对主函数有影响*/
- printf("main: a = %d, b = %d \n",a,b);
- change3(p1,p2); /*方法change3中调换两个数的位置,会对函数主有影响*/
- printf("main: a = %d, b = %d \n",a,b);
- }
相关推荐
IT之家 2020-03-11
graseed 2020-10-28
zbkyumlei 2020-10-12
SXIAOYI 2020-09-16
jinhao 2020-09-07
impress 2020-08-26
liuqipao 2020-07-07
淡风wisdon大大 2020-06-06
yoohsummer 2020-06-01
chenjia00 2020-05-29
baike 2020-05-19
扭来不叫牛奶 2020-05-08
hxmilyy 2020-05-11
黎豆子 2020-05-07
xiongweiwei00 2020-04-29
Cypress 2020-04-25
冰蝶 2020-04-20