I'm having a bit trouble deducing what is const, when applied to pointers to pointers, etc. i.e., what is const when you have
const Foo **foo;
Can I here change something in **foo ? as in foo[0]->bar = 12;
What about:
const Foo ***foo;
Foo **const foo;
You could use cdecl to understand what a C declaration means.
const int **foo;
declare foo as pointer to pointer to const int
thus you can change pointers, but not the value they're pointing to.
int * const * const foo;
declare foo as const pointer to const pointer to int
this, instead, is a cosnt pointer, pointing to a const pointer, to a non-const int: you cannot change pointed value, but it can be changed.
C uses the Clockwise/Spiral Rule, in a case where you have only modifiers at the left side of the variable (of foo) you read stuff going from right to left:
int(5) *(4) const(3) *(2) const(1) foo;
foo
is a constant(1) pointer(2) to constant(3) pointer(4) to an integer(5).
int(6) const(5) *(4) const(3) *(2) const(1) foo;
const(6) int(5) *(4) const(3) *(2) const(1) foo; // [same as above]
In this case foo
is a constant(1) pointer(2) to constant(3) pointer(4) to a constant(5) integer(6) [or to an integer(5) which is constant(6)].
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