42

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++).

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...

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

0

7 Answers 7

68

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

Sign up to request clarification or add additional context in comments.

4 Comments

It's also part of the reason you will sometimes see the alternative "spelling recommended: MyStructure const * *ppMyStruct; Then you can read right-to-left: pointer to pointer to const Mystructure.
Who reads RTL? Surely none of my customers :-) I think we don't need RTL support, do we?
This is an old post so probably my comment-qustion will not be answered : How to create a const pointer to a const pointer to a const MyStructure?
@tchronis, I haven't tested (only that it compiles), but try something like: MyStructure const * const * const ptrMyStr;
65

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

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

5 Comments

Very useful. Why isn't this tool well-known?
Do you happen to know if it's available for windows?
It is open source and afaik it doesnt have any specific requirements to the OS. When lucky it should be compileable with any compiler, in worst case you have to use the gcc/cygwin or mingw stuff.
@flolo you are a hero! I have never heard of that tool! Thank you for sharing it! +1
Online version at cdecl.org Works the other way round too! Enter the English, get the code.
25

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

2 Comments

What about MyStructure const* *ppMyStruct;?
@tobi, that's the same as the first line. See stackoverflow.com/q/3694630 (they talk about references, but pointers behave the same).
7

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

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:

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

GCC 4 says:

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

G++ 4 says:

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

2 Comments

Why is this downvoted? I just thought it was better to give a specific example, instead of just telling him that he is right.
guess it was a Visual C++ hater, GNU C++ lover :-)
5

You are right.

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

Comments

3

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:

MyStructure const** ppMyStruct; 

2 Comments

I fail to see how that makes it clearer, but I suppose it's a matter of habit. If the const comes first, I find it just as easy to read it RTL as "a pointer to a pointer to a MyStructure which is const".
when i tried it i got this cdecl> explain struct MyStructure const** ppMyStruct; syntax error Can any one explain why if this is a right form C declaration
0
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 } 

1 Comment

I don't see how this is an answer to the question. Did you misread it?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.