和菜鸟一起学c之函数指针

来源:互联网 发布:日本化妆品推荐知乎 编辑:程序博客网 时间:2024/05/18 11:28

  还有SD卡的文件格式识别还不会,等明天和飞刀一起讨论下,基本的AndroidSD卡的自动挂载已经实现了,可惜只支持FAT格式的,EXT格式的他不支持,添加了那些其他格式的挂载还是不行,主要是识别还不知道怎么去实现。好了,既然这么着,还是把以前看的一些函数指针做个记录吧。因为linux驱动中很多很多都用到了函数指针,一开始我都觉得奇怪,后来才知道这个。都怪自己以前对于指针的一些知识学得太少了,太浅了。

       先看个简单的代码吧:

[html] view plaincopy
  1. #include <stdio.h>  
  2.   
  3. static int max(int a,int b)  
  4.   
  5. {  
  6.   
  7.       if(a > b)  
  8.   
  9.       {  
  10.   
  11.              return a;    
  12.   
  13.       }  
  14.   
  15.        else   
  16.   
  17.        {  
  18.   
  19.               return b;  
  20.   
  21.        }  
  22.   
  23. return 0;  
  24.   
  25. }    
  26.   
  27.    
  28.   
  29. int main(void)  
  30.   
  31. {     
  32.   
  33.        int(*pmax)(int, int);  //函数指针  
  34.   
  35.        int x, y, z;    
  36.   
  37.        pmax = max;  //把函数max的首地址赋值给pmax函数指针  
  38.   
  39.        printf("input two numbers:\n");    
  40.   
  41.        scanf("%d%d",&x, &y);    
  42.   
  43.        z = (*pmax)(x, y);  //调用函数  
  44.   
  45.        printf("maxmum = %d\n",z);    
  46.   
  47. return 0;  
  48.   
  49. }  


 

再看看运行结果:

 

        这里定义了一个函数指针,pmax,然后这个指针指向了max这个函数,然后当执行z = (*pmax)(x, y);时就会执行那个比较大小的函数了,然后就有了上面的结果了。

      

       好了,接下去我们看看比较复杂的,本人写得搓搓的代码:

[html] view plaincopy
  1. #include <stdio.h>  
  2.   
  3. #include <math.h>  
  4.   
  5. #include <stdlib.h>  
  6.   
  7.    
  8.   
  9. struct point  
  10.   
  11. {  
  12.   
  13.        int x;  
  14.   
  15.        int y;  
  16.   
  17. };  
  18.   
  19.    
  20.   
  21. struct my_point_ops  
  22.   
  23. {  
  24.   
  25.        struct point *p;  
  26.   
  27.        double (*line_Length)(struct point p1, struct point p2);  
  28.   
  29.        int (*draw_Point)(struct point *p);  
  30.   
  31. };  
  32.   
  33.    
  34.   
  35. double my_line_length(struct point p1, struct point p2)  
  36.   
  37. {  
  38.   
  39.        return ((p1.x-p2.x)*(p1.x-p2.x) + (p1.y-p2.y)*(p1.y-p2.y));  
  40.   
  41. }  
  42.   
  43.    
  44.   
  45. int my_draw_point(struct point *p)  
  46.   
  47. {  
  48.   
  49.        return p->x + p->y;  
  50.   
  51. }  
  52.   
  53.    
  54.   
  55. struct my_point_ops mops = {  
  56.   
  57.        .line_Length = my_line_length,  
  58.   
  59.        .draw_Point = my_draw_point  
  60.   
  61. };  
  62.   
  63.    
  64.   
  65. int main(void)  
  66.   
  67. {  
  68.   
  69.        struct point p1 = {.x = 5.y = 2};  
  70.   
  71.        struct point p2 = {.x = 1.y = 8};  
  72.   
  73.        p3 = (struct point *)malloc(sizeof(struct point));  
  74.   
  75.        p3->x = 3;  
  76.   
  77.        p3->y = 4;  
  78.   
  79.        printf("%lf\n", mops.line_Length(p1, p2));  
  80.   
  81.        printf("%d\n", mops.draw_Point(p3));  
  82.   
  83.        free(p3);  
  84.   
  85. return 0;  
  86.   
  87. }  


不知道为什么Cfree运行错了,用GCC编译时过了的。这个就不纠结了。

 

首先看看这个my_point_ops结构体

 

[html] view plaincopy
  1. struct my_point_ops  
  2. {  
  3.     struct point *p;  
  4.     double (*line_Length)(struct point p1, struct point p2);  
  5.     int (*draw_Point)(struct point *p);  
  6. };  



      里面定义了两个函数指针。驱动里的代码越来越觉得有面向对象的概念了,什么都是一个一个对象一个结构了。

 

然后接下来看看这个,这个其实就是初始化了,其函数指针line_Length指向了my_line_length函数,函数指针draw_Point =指向了my_draw_point函数。

[html] view plaincopy
  1. struct my_point_ops mops = {  
  2.   
  3.        .line_Length = my_line_length,  
  4.   
  5.        .draw_Point = my_draw_point  
  6.   
  7. };  


在看最后

[html] view plaincopy
  1. printf("%lf\n", mops.line_Length(p1, p2));  
  2.   
  3. printf("%d\n", mops.draw_Point(p3));  


当调用mmops的成员函数mops.line_Length(p1, p2)其实就是调用了

[html] view plaincopy
  1. double my_line_length(struct point p1, struct point p2)  
  2.   
  3. {  
  4.   
  5.        return ((p1.x-p2.x)*(p1.x-p2.x) + (p1.y-p2.y)*(p1.y-p2.y));  
  6.   
  7. }  


而当调用mmops的成员函数mops.draw_Point(p3)其实就是调用了

[html] view plaincopy
  1. int my_draw_point(struct point *p)  
  2.   
  3. {  
  4.   
  5.        return p->x + p->y;  
  6.   
  7. }  


简单吧,就是这样的。哈哈。。。。

0 0
原创粉丝点击