函数指针赋值

zer*_*ola 2 c pointers function

为什么我能够分配一个函数,该函数返回一个没有为函数指针指定参数的int,该函数指针返回一个int但是接受一个int参数?

例如,当我这样做时:

int f()
{ return 0;}

int (*ptr)(int) = f;
Run Code Online (Sandbox Code Playgroud)

编译器没有给我任何警告

Ker*_* SB 5

在C中,f不是"没有参数",而是"任何参数"*.说int f(void)声明"没有参数".

这与C++不同.值得注意的是,C具有功能"声明"和功能"原型"的单独概念:

int f();              /* declaration -- f is still a mystery */
int f(int, double);   /* prototype -- now we know how to call f */
int f(int n, double c) { return -1; }  /* defintion -- now we can link */
Run Code Online (Sandbox Code Playgroud)

*)正如我在评论中所说,"任何参数"仅限于不受默认促销的类型(与默认促销发生在可变参数上的方式相同).也就是说,float中,所有的口味charshort int,而且...,是不是在实际的函数签名允许.