函式指標在 C 中


函式指標指向程式碼,就像普通的指標一樣。

在函式指標中,可以使用函式名稱來獲取函式的地址。

函式也可以作為引數傳遞,並可以從函式中返回。

宣告

function_return_type(*Pointer_name)(function argument list)

範例

 實際演示

#include<stdio.h>
int subtraction (int a, int b) {
   return a-b;
}
int main() {
   int (*fp) (int, int)=subtraction;
   //Calling function using function pointer
   int result = fp(5, 4);
   printf(" Using function pointer we get the result: %d",result);
   return 0;
}

輸出

Using function pointer we get the result: 1

更新日期:30-Jul-2019

4K+ 瀏覽量

事業 起步

完成教程後獲得認證

立即開始
廣告