更多的是好奇心。基本上我想知道是否可以在一行中声明多个函数指针,例如:
int a = 1, b = 2;
用函数指针?无需求助于 typedef
。
我试过 void (*foo = NULL, *bar = NULL)(int)
。毫不奇怪,这没有用。
最佳答案
尝试如下:
void (*a)(int), (*b)(int);
void test(int n)
{
printf("%d\n", n);
}
int main()
{
a = NULL;
a = test;
a(1);
b = test;
b(2);
return 0;
}
编辑:
另一种形式是函数指针数组:
void (*fun[2])(int) = {NULL, NULL};
void test(int n)
{
printf("%d\n",n);
}
int main()
{
fun[0] = NULL;
fun[0] = test;
fun[0](1);
fun[1] = test;
fun[1](2);
}
关于c - 如何在没有类型定义的情况下在一行中声明多个函数指针?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18414893/