3

I'm reading appendix A of Accelerated C++. There the authors show an example of a declaration which looks like this:

const char * const * const * cp;

They say const char is the specifier and * const * const * cp is the declarator. I'm confused about the purpose of the extra const and *s. Is this a declaration of a const pointer to a const char?

1
  • 2
    It's a declaration of a "pointer to const pointer to const pointer to const char", I believe. And this is why C++. Commented May 29, 2013 at 19:09

4 Answers 4

7

It's the declaration of

  • a pointer
  • to a const pointer
  • to a const pointer
  • to const char

Thus you may change cp, but you may not change any of

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

6 Comments

And why would anyone ever want to do this?
@crush Sadism? I have never used triple pointers yet, I don't know when you would need them.
It is exceedingly rare that I find an instance where a double pointer is useful.
@crush Rare, but that happens, consider for example long int strtol(const char *nptr, char **endptr, int base); and friends.
Yeah, so I guess a triple pointer might be used in conjunction with an array of strings where you are pointing to a specific character of a specific string in that array? There are certainly better ways to handle this that take advantage of C++
|
4

As we can see from cdecl, cp is a pointer to const pointer to const pointer to const char.

You can see this by breaking it down right-to-left:

const char * const * const * cp;
                             cp is
                           a pointer
                   to const pointer
           to const pointer
to const char

Also, the standard (§ 8) says that:

The specifiers indicate the type, storage class or other properties of the entities being declared. The declarators specify the names of these entities and (optionally) modify the type of the specifiers with operators such as * (pointer to) and () (function returning).

Comments

1

From cdecl.org:

declare cp as pointer to const pointer to const pointer to const char

Comments

1

This is the same as char * * * cp except that all the pointers are const, except one. Since char***cp is a pointer to a pointer to a pointer your example is the same but it is a pointer to a const pointer to a const pointer

4 Comments

Formatting-cat got your tongue? ;)
Not anymore, the stars made some of the text bold
"all the pointers are const" - no, two pointers are const, one pointers is non-const
@kotlomoy fixed it but notice that I had it correct in the second half previously

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.