c - 如何在没有类型定义的情况下在一行中声明多个函数指针?

标签 c pointers function-pointers

更多的是好奇心。基本上我想知道是否可以在一行中声明多个函数指针,例如:

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/

相关文章:

c - 超出时间限制错误。这是什么意思?

c - 为什么不能只检查 errno 是否等于 ERANGE?

c++ - 如何从成员访问 protected /私有(private)的嵌套类指针

c - 确定存储指向其他不同维度数组的指针的数组的类型

c - 如何在 C 中打印函数指针的值?

c++ - std::function 可以用来存储带有可变参数的函数吗

c - 通过 arduino 的 ENC28J60 模块发布 JSON 时出现奇怪的行为

c++ - 我可以使用 nullptr 作为 Linux 系统调用参数吗?

c++ - 将 C++ 成员函数设置为 DLL 中的函数

c++ - 声明 const 成员函数的语法返回一个裸函数指针,没有 typedefs?