c - 没有参数类型的函数指针?

标签 c pointers

我试图声明一个函数指针,指向任何返回相同类型的函数。我省略了指针声明中的参数类型以查看将生成什么错误。但是程序编译成功,执行没有任何问题。

这是正确的声明吗?我们不应该指定参数类型吗?

#include <stdio.h>
#include <stdlib.h>

void add(int a, int b)
{
    printf("a + b = %d", a + b);
}

void (*pointer)() = &add;

int main()
{
    add(5, 5);
    return 0;
}

输出:

a + b = 10

最佳答案

类型名称中的空括号表示未指定的参数。请注意,这是一个过时的功能。

C11(ISO/IEC 9899:201x) §6.11.6 Function declarators

The use of function declarators with empty parentheses (not prototype-format parameter type declarators) is an obsolescent feature.

关于c - 没有参数类型的函数指针?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20835534/

相关文章:

c - 我正在创建我自己的 UNIX shell,有时当我运行 'ls' 命令时它会给出一个错误地址错误

c++ - 如何将指针作为函数参数返回

C++ 安全返回指向迭代器的指针以进行 map 插入?

c++ - DsGetDomainControllerInfo 返回 "Pointer to a pointer variable that receives an array"?我不明白

c - C语言中反转句子,同时保持空格位置

c++ - 带有 gtest(C++) 的 CMakeLists C 程序

使用 ExitThread 关闭线程 - C

C++ 遍历成员函数

c++ - 并排定义指针和类型问题

c - 如何在 Windows-10 的 C 控制台程序中获取鼠标输入?