Difference between const int*, const int * const and int * const in C++
Hello! I've just started learning C++ and am trying to understand pointers and the const qualifier. I've read several articles, but I'm still confused about these three constructs: `const int*`, `int * const`, and `const int * const`. \n\nFor example, I wrote this code:\n\n```cpp\nint value = 5;\nconst int* ptr1 = &value;\nint * const ptr2 = &value;\nconst int * const ptr3 = &value;\n```\n\nI understand that they all have something to do with whether the data or the pointer itself cannot be changed, but I can't clearly articulate the difference. When I try to assign a new value through `*ptr1 = 10;`, the compiler complains. And if I try to change `ptr2`, for example `ptr2 = &otherValue;`, I also get an error. But I can redirect `ptr1` to another variable, while I cannot do that with `ptr2`. Why? \n\nI also want to understand in which cases to use which variant.
No answers yet.
Log in to leave an answer