Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Confusing pointer declaration

Tags:

c++

pointers

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?

like image 420
user2433857 Avatar asked Aug 05 '26 17:08

user2433857


2 Answers

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
like image 89
Daniel Fischer Avatar answered Aug 08 '26 10:08

Daniel Fischer


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

like image 25
Bill Avatar answered Aug 08 '26 08:08

Bill



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!