gpt4 book ai didi

有人可以解释为什么 "void func_dec(void (*)(int) funcptr);"是非法的

转载 作者:太空宇宙 更新时间:2023-11-04 01:21:30 24 4
gpt4 key购买 nike

如下所述,当声明一个将函数指针作为参数的函数时,编译器会抛出错误。

 void func_dec(int x, void(*)() funcptr);

鉴于它接受以下声明,

 void func_dec(int x, void(*funcptr)());

为什么编译器无法识别前面的声明,尽管它看起来合乎逻辑。

最佳答案

它是非法的,因为语言标准中的正式定义是这样说的。至于为什么会这样,而且真的很晦涩难懂,原因在这里:

来自 The New C Standard: An Economic and Cultural Commentary (v 1.2 from June 24, 2009, section 6.7 Declarations) :

The intent of this syntax is for an identifier’s declarator to have the same visual appearance as an instance of that identifier in an expression. For instance, in:

int x[3], *y, z(void);

char (*f(int))[];

the identifier x might appear in the source as an indexed array, y as a dereferenced pointer, and z as a function call. An example of an expression using the result of a call to f is (*f(42))[1].

The Development of the C Language by Dennis M. Ritchie相同:

Thus,

int i, *pi, **ppi;

declare an integer, a pointer to an integer, a pointer to a pointer to an integer. The syntax of these declarations reflects the observation that i, *pi, and **ppi all yield an int type when used in an expression. Similarly,

int f(), *f(), (*f)();

declare a function returning an integer, a function returning a pointer to an integer, a pointer to a function returning an integer;

int *api[10], (*pai)[10];

declare an array of pointers to integers, and a pointer to an array of integers. In all these cases the declaration of a variable resembles its usage in an expression whose type is the one named at the head of the declaration.

关于有人可以解释为什么 "void func_dec(void (*)(int) funcptr);"是非法的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41459639/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com