完整代码:
#include<stdio.h>
aaa(){
printf("hi");
}
bbb(){
printf("hello");
}
ccc(){
printf("ccc");
}
int main(){
int(*ptr[3])();
ptr[0]=aaa;
ptr[1]=bbb;
ptr[3]=ccc;
ptr[3]();
}
输出将是“再见”。
从代码中我可以看出,int(*ptr[3])()
是某种与 int 相关的数组声明,它也看起来像函数调用。
下面几行代码将函数名赋值给数组,数组中的一 block 可以用于函数调用。
有人可以解释一下声明是什么以及函数调用是如何进行的吗?
最佳答案
当你遇到这样的类型时,你可以使用cdecl
工具来解码它:
$ cdecl explain "int (*p[3])()"
declare p as array 3 of pointer to function returning int
$
注意 ptr
是 cdecl 的保留字,您只需将变量重命名为更基本的名称...
------编辑------
请注意,空参数列表在 C 或 C++ 中并不意味着相同。在 C 中,这意味着具有未知数量参数的函数(没有参数的 C 函数必须声明为 f(void)
)。在 C++ 中,这意味着一个没有参数的函数。
关于c++ - 这个声明是什么意思 "int(*ptr[3])();"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41520438/