Reputation: 63
I tried to search answer for this but I found it very hard to find 'exact' example of this kind. I understand very little about pointers to pointers and I feel there is something more laying under the skin of this than just pointer to something.
So how would you guys translate this?
void free(shame_1 * const * const group_1);
Am I right if I translate it as const group_1 pointer to const pointer to shame_1?
Thanks.
Upvotes: 5
Views: 1841
Reputation: 106096
Generally, just read from right to left:
void free(shame_1 * const * const group_1);
free
takes a parameter called group_1
that's a const
pointer to a const
pointer to a shame_1
object.
T const *
and others use const T*
and they mean the same thing, so whether you see either T const
or const T
you should read it as const T
. In your code, only the pointers are const
anyway.For example:
shame_1 a_shame;
const shame_1* p_a_shame = &a_shame;
free(&p_a_shame);
There's very little utility in this... free()
could have accepted a const
pointer to a shame_1
without an extra level of indirection, and would still have been able to do all the same things to the shame_1
, so it's only useful if free()
has to pass the const
-pointer-to-const
-pointer to some other function (with ultimately there being no good reason for the whole mess).
You can visualise the memory usage / relationships as:
[a_shame] <------ [p_a_shame] <------ [group_1]*
Am I right if I translate it as const group_1 pointer to const pointer to shame_1?
So, to me the "const group_1 pointer" bit is a bit suspect... I think it reads better as "group_1 is a const pointer to..." or "const pointer (called group_1) to...".
Upvotes: 3
Reputation: 5118
shame_1 * const * const group_1;
declares a variable named group_1, whose type is a const pointer (you cannot change where it points at) to another const pointer (same) to a shame_1-type object, whose value you can actually change.
So, for instance, you cannot compile:
group_1 = nullptr;
*group_1 = nullptr;
You can, however, do:
void f(shame_1& group) {
//stuff that modifies group
...
}
f(**group1);
As zakinster commented, having a look at the spiral rule should help you understand this kind of notation.
Upvotes: 5