You have this situation:
+---+ | l | --\ +---+ \ +---+ >--> | k | +---+ / +---+ | a | --/ +---+
I.e. both l
and a
points directly to k
.
When you then do
int **c = a;
you make c
point to what a
is pointing to as well (i.e. it's the same as doing int **c = &k
), but you declare c
to be a pointer to pointer to an int
, but make it point directly to an int
. That means using single dereference (*c
) you get the value of k
, and when you you use double dereference (**c
) you use the value of k
as a pointer (which it isn't).
I assume what you really want is this situation:
+---+ +---+ +---+ | a | --> | l | --> | k | +---+ +---+ +---+
which you will get by doing
void *a = &l;
Then using c
as a pointer to pointer will work because then a
is a pointer to a pointer.
If you continue with the first scenario, where you have
int k;
int *l = &k;
void *a = l;
and then do
int **c = (int **) &a; // The typecast is needed
Then you have the following situation:
+---+ | l | --\ +---+ \ +---+ >--> | k | +---+ +---+ / +---+ | c | --> | a | --/ +---+ +---+
This will work because then c
really is a pointer to a pointer to something.