Wanderer Wanderer - 18 days ago 5
C++ Question

Using double pointers with const

Suppose we have

int *p1;
const int **pp2= &p1;
const int n=13;


// Which means we can't use pp2 to modify the value of the //variable whose address is stored in * pp2.
* pp2 stores the address of some variable (n in this case below)

The book which I am reading -C++ primer says the following statement is perfectly legal

*pp2=&n;


I feel the above statement violates the matter written in comments above.

Could somebody please help clear this. Thanks a lot

Answer

We can understand it with a simple example, we you write below declaration,

const int *p;

It say *p is constant not p. p can store any address pointing to constant integer.

Similarly, const int **p;

It says pointer to pointer to a constant int. you can change p and *p but not **p. *P can hold any address pointing to contant integer like &n in your case and you can change *P with any addresses pointing to constant integer.

Hope it explains you more:::)