和菜鳥一起學c之函數指標

來源:互聯網
上載者:User

      
還有SD卡的檔案格式識別還不會,等明天和飛刀一起討論下,基本的Android的SD卡的自動掛載已經實現了,可惜只支援FAT格式的,EXT格式的他不支援,添加了那些其他格式的掛載還是不行,主要是識別還不知道怎麼去實現。好了,既然這麼著,還是把以前看的一些函數指標做個記錄吧。因為linux驅動中很多很多都用到了函數指標,一開始我都覺得奇怪,後來才知道這個。都怪自己以前對於指標的一些知識學得太少了,太淺了。

      
先看個簡單的代碼吧:

#include <stdio.h>static int max(int a,int b){      if(a > b)      {             return a;        }       else        {              return b;       }return 0;}   int main(void){          int(*pmax)(int, int);  //函數指標       int x, y, z;         pmax = max;  //把函數max的首地址賦值給pmax函數指標       printf("input two numbers:\n");         scanf("%d%d",&x, &y);         z = (*pmax)(x, y);  //調用函數       printf("maxmum = %d\n",z);  return 0;}

 

再看看運行結果:

 

        這裡定義了一個函數指標,pmax,然後這個指標指向了max這個函數,然後當執行z = (*pmax)(x, y);時就會執行那個比較大小的函數了,然後就有了上面的結果了。

      

      
好了,接下去我們看看比較複雜的,本人寫得搓搓的代碼:

#include <stdio.h>#include <math.h>#include <stdlib.h> struct point{       int x;       int y;}; struct my_point_ops{       struct point *p;       double (*line_Length)(struct point p1, struct point p2);       int (*draw_Point)(struct point *p);}; double my_line_length(struct point p1, struct point p2){       return ((p1.x-p2.x)*(p1.x-p2.x) + (p1.y-p2.y)*(p1.y-p2.y));} int my_draw_point(struct point *p){       return p->x + p->y;} struct my_point_ops mops = {       .line_Length = my_line_length,       .draw_Point = my_draw_point}; int main(void){       struct point p1 = {.x = 5, .y = 2};       struct point p2 = {.x = 1, .y = 8};       p3 = (struct point *)malloc(sizeof(struct point));       p3->x = 3;       p3->y = 4;       printf("%lf\n", mops.line_Length(p1, p2));       printf("%d\n", mops.draw_Point(p3));       free(p3);return 0;}

不知道為什麼Cfree運行錯了,用GCC編譯時間過了的。這個就不糾結了。

 

首先看看這個my_point_ops結構體

 

struct my_point_ops{struct point *p;double (*line_Length)(struct point p1, struct point p2);int (*draw_Point)(struct point *p);};

      裡面定義了兩個函數指標。驅動裡的代碼越來越覺得有物件導向的概念了,什麼都是一個一個對象一個結構了。

 

然後接下來看看這個,這個其實就是初始化了,其函數指標line_Length指向了my_line_length函數,函數指標draw_Point =指向了my_draw_point函數。

struct my_point_ops mops = {       .line_Length = my_line_length,       .draw_Point = my_draw_point};

在看最後

printf("%lf\n", mops.line_Length(p1, p2));printf("%d\n", mops.draw_Point(p3));

當調用mmops的成員函數mops.line_Length(p1, p2)其實就是調用了

double my_line_length(struct point p1, struct point p2){       return ((p1.x-p2.x)*(p1.x-p2.x) + (p1.y-p2.y)*(p1.y-p2.y));}

而當調用mmops的成員函數mops.draw_Point(p3)其實就是調用了

int my_draw_point(struct point *p){       return p->x + p->y;}

簡單吧,就是這樣的。哈哈。。。。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.