0

have some issues with const qualifier. I tried to do smth like this:

int a{ 3 };
int *p{ &a };
//const int **pp{ &p }; // FIXME: Compiler demands that p must have a const int *type
const int *const* pp{ &p }; // works here but it means, that I have a pointer to the const pointer to the const value
*pp = nullptr; // doesn't work cause of upper type

Results in error:

<source>:8:5: error: read-only variable is not assignable
*pp = nullptr; // doesn't work cause of upper type
~~~ ^

BUT: if i deal with new it works funny

const int **pp1{ new const int* {&p} }; // Here I have a poiner to pointer to const int, so I can change it
*pp1 = nullptr; // works

So, why does compiler demands a const int* const*? Compiler - MSVC, standart - c++17

UPD:

const int *p{ nullptr };
const int **pp{ &p }; // My intention
*pp = nullptr; // it works cause pointer non - const

But how can i get same result without const in const int *p?

  • const int** is pointer to const int*. – 김선달 Jan 21 '21 at 11:08
  • the two snippets do different things. Its not only `new` that you changed. Please include the error message in the question, it should already say why the first is wrong – 463035818_is_not_an_ai Jan 21 '21 at 11:11
  • If you use const `const int ** pp{ &p };` but if you use `const int *const* pp{ &p };` or `const int *const* const pp{ &p };` it will work if you wanrt to `cout << **pp` but *pp = nullptr will not work cuz `pp` is const. If you want to understand const and pointers I recommend the following link: https://stackoverflow.com/questions/1143262/what-is-the-difference-between-const-int-const-int-const-and-int-const –  Jan 21 '21 at 11:14
  • @rustyx, as i thought, const int** means that all pointers included in it also const (i.e. *(const int**) = const int*) – Shamil Mukhetdinov Jan 21 '21 at 11:14
  • @taiwan12, thank you, i didn't find that question by myself – Shamil Mukhetdinov Jan 21 '21 at 11:16
  • i added the compiler error message for you. Please update if you see a different one. Note that "works" or "works not" are not sufficient to describe a problem. It is always better to show the code/error message instead of rephrasing it – 463035818_is_not_an_ai Jan 21 '21 at 11:26

1 Answers1

4

There are cases where this site https://cdecl.org/ for C syntax can also help with C++. For this:

const int *const* pp 

it tells me:

declare pp as pointer to const pointer to const int

For this:

const int **pp1

it tells me:

declare pp1 as pointer to pointer to const int

Let's use that....

*pp = ... // ERROR !

When you dereference pp then you get a "const pointer to const int" (because pp is "pointer to const pointer to const int"). You cannot assign to a const whatever. What counts it the top level const!

*pp1 ... // OK !?!

When you dereference pp1 you get a "pointer to const int" (because pp1 is a "pointer to pointer to const int"). A pointer to const int is not constant. What counts is the top level const! It points to a const int but the pointer itself is not const.

Conclusion: The difference between your two versions is not the use of new but the type of the pointer that you dereference (and then try to assing to the pointee).

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
  • Thank you but last question. Why compiler force me to do my pointer const when I use const int* const* pp{ &p }? Second const means that a pointer will be const, not value, don't it? – Shamil Mukhetdinov Jan 21 '21 at 11:40
  • @ShamilMukhetdinov `const int * const *` is a constant pointer `int * const *` is a non-constant pointer. The compiler does not force you, it just does exactly what you tell it – 463035818_is_not_an_ai Jan 21 '21 at 11:43
  • @ largest_prime_is_463035818, but why i cannot change pointer itself? Not the pointer value. I update question with some details now – Shamil Mukhetdinov Jan 21 '21 at 11:45
  • 2
    @ShamilMukhetdinov if you update your question my answer will not be the right answer anymore. Please do not change the question after you received asnwers. You cannot assign to the pointer when the pointer is const. Sorry but I dont know how to better explain. If you are still a little confused thats fine, it is confusing – 463035818_is_not_an_ai Jan 21 '21 at 11:52
  • @ largest_prime_is_463035818, thank you, I understood a bit)) – Shamil Mukhetdinov Jan 21 '21 at 11:55