C++ 在C和C+中,指向指针的常量指针是什么意思+;?

C++ 在C和C+中,指向指针的常量指针是什么意思+;?,c++,c,pointers,constants,C++,C,Pointers,Constants,我知道从右到左阅读声明的经验法则,我相当肯定我知道发生了什么,直到一位同事告诉我: const MyStructure** ppMyStruct; 表示“ppMyStruct是指向(可变)MyStructure的常量指针的指针”(在C++中) 我原以为它的意思是“ppMyStruct是指向const MyStructure的指针”。 我在C++规范中寻找答案,但显然我不是很擅长这个…… < C++中的意思是什么?C?/P>> P中的意思是一样的吗?你的同事错了。这是指向常量MyStructur

我知道从右到左阅读声明的经验法则,我相当肯定我知道发生了什么,直到一位同事告诉我:

const MyStructure** ppMyStruct;
表示“ppMyStruct是指向(可变)MyStructure的常量指针的指针”(在C++中)

我原以为它的意思是“ppMyStruct是指向const MyStructure的指针”。 我在C++规范中寻找答案,但显然我不是很擅长这个……
< C++中的意思是什么?C?/P>> P中的意思是一样的吗?你的同事错了。这是指向常量MyStructure的(非常量)指针的(非常量)指针。在C和C++中,

< P>在这种情况下,工具CDECL(或C++ + DECL)可以帮助:

     [flolo@titan ~]$ cdecl explain "const struct s** ppMyStruct"
     declare ppMyStruct as pointer to pointer to const struct s

你的同事错了,C和C++也是一样的。请尝试以下操作:

typedef struct foo_t {
    int i;
} foo_t;

int main()
{
    foo_t f = {123};
    const foo_t *p = &f;
    const foo_t **pp = &p;
    printf("f.i = %d\n", (*pp)->i);
    (*pp)->i = 888; // error
    p->i = 999;     // error
}

Visual C++ 2008给出了最后两行的以下错误:

error C2166: l-value specifies const object
error C2166: l-value specifies const object
GCC 4说:

error: assignment of read-only location '**pp'
error: assignment of read-only location '*p'
G++4说:

error: assignment of data-member 'foo_t::i' in read-only structure
error: assignment of data-member 'foo_t::i' in read-only structure

你的解释是对的。下面是另一种看待它的方式:

const MyStructure *      *ppMyStruct;        // ptr --> ptr --> const MyStructure
      MyStructure *const *ppMyStruct;        // ptr --> const ptr --> MyStructure
      MyStructure *      *const ppMyStruct;  // const ptr --> ptr --> MyStructure
这些都是具有一个常量限定符的指针对指针的替代方法。右到左规则可以用来破译声明(至少在C++中,我不是C专家)。< / P > P >你是对的。< /P>
已经指向“”了。我非常喜欢这一条——虽然有点复杂。

作为其他评论的推论,不要把“const”放在第一位。它真的属于那种类型。这会立即澄清其含义,只需像往常一样阅读RTL即可:

MyStructure const** ppMyStruct;

为什么会被否决?我只是想给出一个具体的例子,而不是仅仅告诉他他是正确的。猜它是一个视觉C++仇恨者,GNU C++爱好者:-非常有用。为什么这个工具不知名?你知道它是否适用于windows吗?它是开源的,而且对操作系统没有任何特定的要求。幸运的是,它应该可以用任何编译器编译,在最坏的情况下,你必须使用gcc/cygwin或mingw的东西。@flolo你是个英雄!我从没听说过那个工具!谢谢分享+1在线版本at也可以反过来工作!输入英语,获取代码。这也是你有时会看到备选方案的部分原因“建议拼写:MyStructure const**ppMyStruct;然后您可以从右向左读取:指向constmystructure的指针。谁读取RTL?当然没有我的客户:-)我想我们不需要RTL支持,是吗?这是一篇老帖子,所以我的评论问题可能不会得到回答:如何创建指向常量MyStructure的常量指针?@tchronis,我还没有测试过(只测试过它的编译),但可以尝试类似的东西:MyStructure const*const*const ptrMyStr;我不明白这怎么会让事情变得更清楚,但我想这是习惯的问题。如果const放在第一位,我发现RTL与“指向MyStructure的指针一样容易阅读,MyStructure是const”。当我尝试它时,我得到了这个cdecl>explain struct MyStructure const**ppMyStruct;语法错误任何人都能解释为什么如果这是一个正确的C格式声明,我不明白这是如何回答这个问题的。你误读了吗?我的结构常量**ppMyStruct@托比,这和第一行一样。请参阅(它们谈论引用,但指针的行为相同)。
void Foo( int       *       ptr,
          int const *       ptrToConst,
          int       * const constPtr,
          int const * const constPtrToConst )
{
    *ptr = 0; // OK: modifies the pointee
    ptr  = 0; // OK: modifies the pointer

    *ptrToConst = 0; // Error! Cannot modify the pointee
    ptrToConst  = 0; // OK: modifies the pointer

    *constPtr = 0; // OK: modifies the pointee
    constPtr  = 0; // Error! Cannot modify the pointer

    *constPtrToConst = 0; // Error! Cannot modify the pointee
    constPtrToConst  = 0; // Error! Cannot modify the pointer
}