三种函数方法的调用,
主要看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);
- printf("main: a = %d, b = %d \n",a,b);
-
- change2(p1,p2);
- printf("main: a = %d, b = %d \n",a,b);
-
- change3(p1,p2);
- printf("main: a = %d, b = %d \n",a,b);
- }