C语言指针参数的引用


C语言指针参数的引用,文中要注意加注释的地方,也是容易出错的地方!

  1. #include <stdio.h>   
  2.   
  3. void change(int *p1, int *p2);  
  4. void order(int *p1, int *p2,int *p3);  
  5.   
  6. int main()  
  7. {  
  8.     int a, b,c;  
  9.     printf("Input number 1:\n");  
  10.     scanf("%d",&a);  
  11.     printf("Input number 2:\n");  
  12.     scanf("%d",&b);  
  13.     printf("Input number 3:\n");  
  14.     scanf("%d",&c);  
  15.       
  16.     printf("a=%d, b=%d, c=%d\n",a,b,c);  
  17.   
  18.     order(&a,&b,&c);  
  19.   
  20.     printf("a=%d, b=%d, c=%d\n",a,b,c);  
  21.   
  22. }  
  23.   
  24. void change(int *p1, int *p2)  
  25. {  
  26.     int temp;  
  27.     if(*p1>*p2)  
  28.     {  
  29.         temp = *p1;  
  30.         *p1 = *p2;  
  31.         *p2 = temp;  
  32.     }  
  33. }  
  34.   
  35. void order(int *p1, int *p2, int *p3)  
  36. {  
  37.     int temp;  
  38.     if(*p1>*p2)  
  39.     {  
  40.         change(p1,p2); //直接p1,p2,不要带星号,p1,p2现在已经是指针变量了,此处易出错   
  41.     }  
  42.     if(*p1>*p3)  
  43.     {  
  44.         change(p1,p3);  //直接p1,p3,不要带星号,p1,p2现在已经是指针变量了,此处易出错   
  45.     }  
  46.     if(*p2>*p3)  
  47.     {  
  48.         change(p2,p3);  //直接p2,p3,不要带星号,p1,p2现在已经是指针变量了,此处易出错   
  49.     }  
  50. }  

相关内容