#include <stdio.h>
int max(int x, int y)
{
return (x >= y ? x : y);
}
typedef int (*FUNC_PTR)();
int main(int argc, char *argv[])
{
int arr[5] = {1, 2, 3, 4, 5};
FUNC_PTR func = NULL;
/* 函数调用 */
func = max;
printf("max(2, 3)=%d \n", func(2, 3));
return 0;
}
上面的代码没有问题,但是,如果将函数指针定义修改为 typedef int (*FUNC_PTR)(void); 则会编译失败。
[Warning] ...\Projects\test\main.c:16: warning: assignment from incompatible pointer type
[Error] ...\Projects\test\main.c:17: error: too many arguments to function
这说明定义函数指针时,(*FUNC_PTR)()并不等同于 (*FUNC_PTR)(void);
另外,可以给(*FUNC_PTR)()定义的函数传递参数。