C語言中的函數指針

C語言中的函數指針


函數指針的概念:   函數指針是一個指向位於代碼段的函數代碼的指針。


函數指針的使用:   


#include<stdio.h>

typedef struct (*fun_t) (int,int);

fun_t pf;

int add(int a, int b)

{

return a+b;

}

int sub(int a,int b)

{

return a-b;

}

int mul(int a,int b)

{

return a*b;

}

int div(int a,int b)

{

return a/b;

}

int main()

{

int result1,result2,result3,result4;

pf = add;

result1 = pf(20,4);

printf("result1 is %d",result);

pf = sub;

result2=pf(20,4);

printf("result2 is %d",result2);

pf = mul;

result3 = pf(20,4);

printf("result3 is %d",result3);

pf = div;

result4=pf(20,4);

printf("result4 is %d",result4);

return 0;

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章