const 指针到指针在 C 和 C++ 中是什么意思?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/336585/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 14:45:13  来源:igfitidea点击:

What does a const pointer-to-pointer mean in C and in C++?

c++cpointersconst

提问by Niklas

I know the rule-of-thumb to read declarations right-to-left and I was fairly sure I knew what was going on until a colleague told me that:

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

const MyStructure** ppMyStruct;

means "ppMyStruct is a pointer to a const pointer to a (mutable) MyStructure" (in C++).

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

I would have thought it meant "ppMyStruct is a pointer to a pointer to a const MyStructure". I looked for an answer in the C++ spec, but apparently I'm not very good at that...

我原以为这意味着“ppMyStruct 是一个指向 const MyStructure 的指针”。我在 C++ 规范中寻找答案,但显然我不太擅长......

What does in mean in C++, and does it mean the same thing in C?

in 在 C++ 中是什么意思,它在 C 中是什么意思?

回答by James Hopkin

Your colleague is wrong. That is a (non-const) pointer to a (non-const) pointer to a const MyStructure. In both C and C++.

你的同事错了。那是一个(非常量)指针,指向一个(非常量)指向一个 const MyStructure 的指针。在 C 和 C++ 中。

回答by flolo

In such cases the tool cdecl (or c++decl) can be helpfull:

在这种情况下,工具 cdecl(或 c++decl)可能会有所帮助:

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

回答by efotinis

You were right in your interpretation. Here's another way to look at it:

你的解释是对的。这是查看它的另一种方式:

const MyStructure *      *ppMyStruct;        // ptr --> ptr --> const MyStructure
      MyStructure *const *ppMyStruct;        // ptr --> const ptr --> MyStructure
      MyStructure *      *const ppMyStruct;  // const ptr --> ptr --> MyStructure

These are all the alternatives of a pointer-to-pointer with one const qualifier. The right-to-left rule can be used to decipher the declarations (at least in C++; I'm no C expert).

这些都是带有一个 const 限定符的指针到指针的所有替代方案。从右到左的规则可用于破译声明(至少在 C++ 中;我不是 C 专家)。

回答by csl

Your colleague is wrong, and it's the same for C and C++. Try the following:

你的同事错了,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 gives the following errors for the last two lines:

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

error C2166: l-value specifies const object
error C2166: l-value specifies const object

GCC 4 says:

GCC 4 说:

error: assignment of read-only location '**pp'
error: assignment of read-only location '*p'

G++ 4 says:

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

回答by xtofl

Youare right.

是对的。

Another answeralready pointed to the "Clockwise Spiral Rule". I liked that one very much - a little elaborate, though.

另一个答案已经指向“顺时针螺旋规则”。我非常喜欢那个——不过有点复杂。

回答by MSalters

As a corollary to the other comments, don't put 'const' first. It really belongs after the type. That would have clarified the meaning immediately, just read it RTL as usual:

作为其他评论的必然结果,不要将“const”放在首位。它确实属于类型之后。那会立即澄清含义,只需像往常一样阅读RTL:

MyStructure const** ppMyStruct;

回答by MSalters

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
}