I am learning c, and I am quite confused about this double pointer question.
int x = 44;
int *p = &x;
int **t = &p;
bool a = (*t = &x);
I need to tell whether a
will be true or false
, and the correct answer is true
. My thoughts were that t
points to the address of p
, and p
points to the address of x
. I know if you put **t
, it should point to the address of x
, but I thought if you just put *t
it should point to the address of p
. Can anyone explain?
int x = 44;
Declares integer variable x, which stores value of 44.
int *p = &x;
Declares integer pointer variable called p, p now stores the address of x.
int **t = &p;
Declares pointer to pointer of type int called t, t stores the address of p. (Pointers have addresses too)
bool a = (*t = &x);
In C;
'*' = Extracts the value from an address (Dereference)
'&' = Gives address of variable (Reference)
Since t
is a pointer to the value stored in p
. *t
will be the value stored in p
, which is the address of x
. (We figured this out in the second line)
On the other hand since the &
is used on variable x
. This will extract the address of x
.
Therefore *t == &x
, which sets the boolean value a to true.