2

a pointer to a constant int. I may change the pointer, but I may not change the value:

const int* a; 

a constant pointer to an int. I may not change the pointer, but I may change the variable's value:

int* const a; 

now, how do things look like if I'm dealing with a pointer to a pointer?

int** a; 

how do I:

a) declare a const pointer to a non-const pointer to a non-const int

b) declare a non-const pointer to a const pointer to a non-const int

c) declare a non-const pointer to a non-const poiinter to a const int ?

5
  • I suggest typedefs. Lots of typedefs. Lots of typedefs that build on one another. Enough typedefs that you hate life to such a degree that you avoid such foolishness in the future. Yes, the distinction could be important. No, I have never seen production code where it mattered. Commented Nov 6, 2019 at 21:06
  • To be fair: "I have never NOTICED code where it mattered", in my 20+ year career as a programmer, most of which has been spent in C++. Commented Nov 6, 2019 at 21:06
  • this is more for my understanding of const-correctness than in order to write production code. I did stumble over it when I tried to replace a statically allocated static const char* foo[1000] with a dynamically allocated static char** foo and the compiler didn't allow me to assign string literals to the elements of foo Commented Nov 6, 2019 at 21:09
  • The const keyword binds to the thing to its immediate left. (Unless const appears very first, in which case it binds to the thing to its immediate right.) Commented Nov 6, 2019 at 22:13
  • It’s worth noting that const-correctness isn’t a purely academic question if you write for embedded systems. In embedded applications, the const keyword signals an intention that the item should be stored in the device’s Program ROM rather than copied from that ROM to program RAM on startup. Most embedded systems have more ROM than RAM, so this is an important distinction when you’re close to the system’s memory limits. Commented Jan 3, 2024 at 12:25

2 Answers 2

3

a) declare a const pointer to a non-const pointer to a non-const int

int ** const a = nullptr; 

Note that, since the pointer above is const-qualified it has to be initialized at the declaration. Otherwise, it won't compile.

b) declare a non-const pointer to a const pointer to a non-const int

int * const *b; 

c) declare a non-const pointer to a non-const pointer to a const int

const int **c; 

The pattern is, for a const pointer:

T * const ptr; // const pointer to T 

and for a non-const pointer:

T * ptr; // non-const pointer to T 

Then, select the proper pattern above for the outtermost pointer and replace T accordingly for the innermost pointer (i.e., the deepest one buried in the type):

  • Non-const pointer to a non-const int: T = int *.
  • const pointer to a non-const int: T = int * const.
  • Non-const pointer to a const int: T = const int *.
Sign up to request clarification or add additional context in comments.

Comments

0

It's all about where theconst is placed relative to the *: if const is on the left then what's pointed to is const, if const is on the right of * then what's pointing is const.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.