1. 程式人生 > >c語言——函式指標

c語言——函式指標

  函式具有可賦值給指標的實體記憶體地址,一個函式的函式名就是一個指標,它指向函式的程式碼。一個函式的地址是該函式的進入點,也是呼叫函式的地址。函式的呼叫可以通過函式名,也可以通過指向函式的指標來呼叫。函式指標還允許將函式作為變元傳遞給其他函式。

      不帶括號和變數列表的函式名,這可以表示函式的地址,正如不帶下標的陣列名可以表示陣列的首地址。

定義形式:

        型別 (*指標變數名)(引數列表);

例如:

        int (*p)(int i,int j);

    p是一個指標,它指向一個函式,該函式有2個整形引數,返回型別為int。p首先和*結合,表明p是一個指標。然後再與()結合,表明它指向的是一個函式。指向函式的指標也稱為函式指標。

        例1:

  1. #include <stdio.h>

  2. #define GET_MAX 0

  3. #define GET_MIN 1

  4. int get_max(int i,int j)

  5. {

  6. return i>j?i:j;

  7. }

  8. int get_min(int i,int j)

  9. {

  10. return i>j?j:i;

  11. }

  12. int compare(int i,int j,int flag)

  13. {

  14. int ret;

  15. //這裡定義了一個函式指標,就可以根據傳入的flag,靈活地決定其是指向求大數或求小數的函式

  16. //便於方便靈活地呼叫各類函式

  17. int (*p)(int,int);

  18. if(flag == GET_MAX)

  19. p = get_max;

  20. else

  21. p = get_min;

  22. ret = p(i,j);

  23. return ret;

  24. }

  25. int main()

  26. {

  27. int i = 5,j = 10,ret;

  28. ret = compare(i,j,GET_MAX);

  29. printf("The MAX is %d\n",ret);

  30. ret = compare(i,j,GET_MIN);

  31. printf("The MIN is %d\n",ret);

  32. return 0 ;

  33. }

        例2:

  1. #include <stdio.h>

  2. #include <string.h>

  3. void check(char *a,char *b,int (*cmp)(const char *,const char *));

  4. main()

  5. {

  6. char s1[80],s2[80];

  7. int (*p)(const char *,const char *);

  8. //將庫函式strcmp的地址賦值給函式指標p

  9. p=strcmp;

  10. printf("Enter two strings.\n");

  11. gets(s1);

  12. gets(s2);

  13. check(s1,s2,p);

  14. }

  15. void check(char *a,char *b,int (*cmp)(const char *,const char *))

  16. {

  17. printf("Testing for equality.\n");

  18. //表示式(*cmp)(a,b)呼叫strcmp,由cmp指向庫函式strcmp(),由a和b作呼叫strcmp()的引數。

  19. //呼叫時,與宣告的情況類似,必須在*cmp周圍使用一對括號,使編譯程式正確操作,

  20. //同時這也是一種良好的編碼風格,指示函式是通過指標呼叫的,而不是函式名。

  21. if((*cmp)(a,b)==0)

  22. printf("Equal\n");

  23. else

  24. printf("Not Equal\n");

  25. }


        例3:

  1. #include <stdio.h>

  2. #include <ctype.h>

  3. #include <stdlib.h>

  4. #include <string.h>

  5. //check()函式的第3個函式是函式指標,就可以根據具體情況傳入不同的處理函式

  6. void check(char *a,char *b,int (*cmp)(const char *,const char *));

  7. //自定義的比較兩個字串的函式

  8. int compvalues(const char *a,const char *b);

  9. main()

  10. {

  11. char s1[80],s2[80];

  12. printf("Enter two values or two strings.\n");

  13. gets(s1);

  14. gets(s2);

  15. //如果是數字,則用函式指標傳入數字比較函式進行處理

  16. if(isdigit(*s1)){

  17. printf("Testing values for equality.\n");

  18. check(s1,s2,compvalues);

  19. }

  20. //如果是字串,則用函式指標傳入庫函式strcmp進行處理

  21. else{

  22. printf("Testing strings for equality.\n");

  23. check(s1,s2,strcmp);

  24. }

  25. }

  26. void check(char *a,char *b,int (*cmp)(const char *,const char *))

  27. {

  28. if((*cmp)(a,b)==0)

  29. printf("Equal.\n");

  30. else

  31. printf("Not Equal.\n");

  32. }

  33. int compvalues(const char *a,const char *b)

  34. {

  35. if(atoi(a)==atoi(b))

  36. return 0;

  37. else

  38. return 1;

  39. }


注意:

        int *f(int i, int j);

        int (*p)(int i, int j);

    前者是返回值是指標的函式;後者是一個指向函式的指標。