代码之家  ›  专栏  ›  技术社区  ›  Cedric H.

模糊指针声明

c c++
  •  6
  • Cedric H.  · 技术社区  · 15 年前

    我有一个问题,在某种程度上,我想,完全无关紧要:那是什么(为什么)?

    const float *(*const*)(int)
    

    对吗?

    如何“心理解析” (*const*) ? 尤其是因为没有名字,一开始我不知道从哪里开始。我认为“名字”的唯一可能就是这样说: *const *name

    这个推理有效吗?

    谢谢!

    3 回复  |  直到 15 年前
        1
  •  6
  •   Johannes Schaub - litb    15 年前

    是的,推理是正确的。将它放在所有不在函数参数列表中的“*”的右侧,以及所有不在函数参数列表中的[]的左侧。那你有

    const float *(*const* name)(int)
    

    然后照常读。从这里,甚至如何找到正确的名字, there are 许多教程如何解析这个在互联网上。

    对于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
    

    然后可以对它的语法进行分析

    < 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 );
    

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

        2
  •  12
  •   Cubbi    15 年前

    你说得对,这个名字跟在后面 *const* const float *(*const* name)(int) 在里面 cdecl.org ,它会告诉你 "

    至于心理分析,我只记得 R (*p)(A) R (**p)(A) 是一个指向函数的指针,此时所需的只是记住 const *

        3
  •  2
  •   CB Bailey    15 年前

    从外到内工作。

    你有这样的东西:

    U D(V)
    

    所以你有类似的功能。

    某种东西是一种功能 int 又回来了 const float* .

    (*const*) 你倒着工作来得到 const 在正确的地方(尽管对称在这里并不重要!)。 *const* int * const * p p 是指向 常数 指向 内景

    内景 又回来了 const float * .