I know the rule-of-thumb to read declarations right-to-left and I was fairly sure I knew what was going on until a colleague told me that:
const MyStructure** ppMyStruct;
means "ppMyStruct is a pointer to a const pointer to a (mutable) MyStructure" (in C++).
I would have thought it meant "ppMyStruct is a pointer to a pointer to a const MyStructure". I looked for an answer in the C++ spec, but apparently I'm not very good at that...
What does in mean in C++, and does it mean the same thing in C?
constant pointer means the pointer is not allowed to modify whereas in pointer to constant, pointer is allowed to modify but the value cannot be modified.
A pointer to a non-const value can change the value it is pointing to. These can not point to a const value.
A pointer to constant is defined as : const <type of pointer>* <name of pointer> An example of definition could be : const int* ptr; Lets take a small code to illustrate a pointer to a constant : #include<stdio.h> int main(void) { int var1 = 0; const int* ptr = &var1; *ptr = 1; printf("%d\n", *ptr); return 0; }
Constant pointer refers to a particular variable in its lifetime while a Constant variable always contain same value in its life time. A constant variable is a variable to which value can be assigned only once. Any try to change its value through any operation or reassignment would result in an error.
Your colleague is wrong. That is a (non-const) pointer to a (non-const) pointer to a const MyStructure. In both C and C++.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With