函数存储在一段内存中,必然也有地址,就有了指向函数的指针,称之为函数指针。
#include int add(int { return } int main() { int(*pf)(int ,int); pf = add; int a = (*pf)(1,1); printf(“%d\n”,a); return 0; } |
有了函数指针,我们可以把函数当类似变量来传递了,参见下面的示例
#include int add(int { return } int sub(int { return } int cou(int (*pf)(int,int),int { return (*pf)(x, y); } int main() { int a = cou(add, 1, 1); int b = cou(sub, 8, 4); printf(“%d\n”, a); printf(“%d\n”, b); return 0; } |