三種函數方法的調用,
主要看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);
- }