从上边程序,我们可以得到下边结论:
1. 函数指针和函数名本质上是一样的,都是指向函数调用地址的指针,只是函数名是常量指针,函数指针是变量。
2. 调用函数的写法func()、(*func)()均可,而我们大多数情况下都会写成前者,应该是(C/C++标准制定者)为了方便大家对函数的调用。
博文Function Pointers and Callbacks in C — An Odyssey如此说:
A pointer is a special kind of variable that holds the address of another variable. The same concept applies to function pointers, except that instead of pointing to variables, they point to functions. If you declare an array, say, int a[10]; then the array name a will in most contexts (in an expression or passed as a function parameter) “decay” to a non-modifiable pointer to its first element (even though pointers and arrays are not equivalent while declaring/defining them, or when used as operands of the sizeof operator). In the same way, for int func();, func decays to a non-modifiable pointer to a function. You can think of func as a const pointer for the time being.
// simple typedef
typedef unsigned long ulong;
// the following two objects have the same type
unsigned long l1;
ulong l2;
// more complicated typedef
typedef int int_t, *intp_t, (&fp)(int, ulong), arr_t[10];
// the following two objects have the same type
int a1[10];
arr_t a2;
// common C idiom to avoid having to write "struct S"
typedef struct {int a; int b;} S, *pS;
// the following two objects have the same type
pS ps1;
S* ps2;