模糊指针声明

Ced*_* H. 6 c c++

我有一个问题,在某种程度上,我想,完全是微不足道的:那是什么(以及为什么)?

const float *(*const*)(int)
Run Code Online (Sandbox Code Playgroud)

我的理解是它是一个"指向一个函数的常量指针的指针,该函数将一个int作为参数并返回一个指向常量float的指针".

这是对的吗 ?

如何"精神上解析" (*const*)?特别是因为没有名字,起初我不知道从哪里开始.我认为"名称"的唯一可能性就是这样:*const *name因为其他组合无效(如果我是正确的),那么"name是指向常量指针的指针......".

这种推理有效吗?

谢谢 !

Cub*_*bbi 12

你的名字是正确的*const*.如果你const float *(*const* name)(int)cdecl.org中插入这行,它会告诉你它意味着"将name声明为指向const指针的指针(int)返回指向const float的指针 "

至于心理解析,我只记得那R (*p)(A)是一个指向函数的指针.因此R (**p)(A)是一个指向函数指针的指针,此时所需要的只是记住如何const*交互.


Joh*_*itb 6

是的,推理是有效的.将它放在所有'*'的右边,这些'*'不在函数参数列表中,而在所有[]'es的左边,不在函数参数列表中.那你有

const float *(*const* name)(int)
Run Code Online (Sandbox Code Playgroud)

然后像往常一样阅读.从那以及如何找到名称的正确位置,很多教程如何在互联网上解析这个.

对于C++,您可能需要查看geordi.

< litb> geordi: -c int name;
< geordi> Success
< litb> geordi: make name a const float *(*const*)(int) and show
< geordi> -c float const *(*const* name)(int) ;
< litb> geordi: << TYPE_DESC(const float *(*const*)(int))
< geordi> pointer to a constant pointer to a function taking an integer 
          and returning a pointer to a constant float
Run Code Online (Sandbox Code Playgroud)

然后,您可以对其语法进行分析

< litb> geordi: show parameter-declaration and first decl-specifier-seq
< geordi> `int` and `float const`.
< litb> geordi: make name an array of 2 pointer to function taking int and 
        returning pointer to float and show
< geordi> -c float const *(* name[2])(int );
Run Code Online (Sandbox Code Playgroud)

它可以让你把C和英语混在一起

< litb> geordi: make name an array of 2 pointer to (float const*(int)) and show
< geordi> -c const float *(* name[2])(int);
Run Code Online (Sandbox Code Playgroud)

如你所见,它非常强大.