我试图创建一个函数指针数组,这样我就可以使用指针打印出函数的输出.(参见有效C语言手册中的练习.)
#include <stdio.h>
#include <stdlib.h>
int a(void) { return 1; }
int b(void) { return 2; }
int c(void) { return 3; }
int main(void)
{
int(*func_arr)[3] = {&a, &b, &c};
printf("%d\n", (*func_arr)[0]);
}
然而,当我编译它时,我得到了警告
starting.c:10:26: warning: incompatible pointer types initializing 'int (*)[3]' with an expression of type
'int (*)(void)' [-Wincompatible-pointer-types]
int(*func_arr)[3] = {&a, &b, &c};
^~
starting.c:10:30: warning: excess elements in scalar initializer
int(*func_arr)[3] = {&a, &b, &c};
^~
当我运行程序时,我得到的输出是-443987883,而它应该是1.
有人知道这个问题的解决方案吗?非常感谢.