Reputation: 141
int age;
int *p;
int **q;
p=&age;
q=&p;
For these set of variables, I'm trying to figure out the logic of how the pointers work w.r.t each other. I have coded it but I want to mentally be able to work out different scenarios using pointer logic which I am struggling with.
Case 1: age == *p
So, a pointer points to a location in memory and stores the address of a variable. So, my thinking is that since p is a variable and *p stores it address and we also know that p=&age (p has address of age). So, *p ==age because they essentially have the same address.
Case 2: age == **q
For this I drew a diagram.
I had *q stores address of **q and **q stores address of variable q. So, since variable q = &p and p = & age, so q=& age and thus making the statement age==*q true
Case 3: age == *q
This is false but I'm not sure why. *q stores address of q which is the same as &p and since age is same as p which is same as q, it should be equal? I compiled it and its different but I dont see why though.
Case 4: p ==*q
This again is True. q=&p so q* stores q's address which is basically the address of p.
This is how I see things for this question. Any guidance/correction on how to approach this and any flaws in my logic would help a lot.
Upvotes: 0
Views: 41
Reputation: 180113
Case 1: age == *p
So, a pointer points to a location in memory and stores the address of a variable. So, my thinking is that since p is a variable and *p stores it address and we also know that p=&age (p has address of age). So, *p ==age because they essentially have the same address.
True, and your reasoning is essentially correct. I would phrase the last bit differently, however: since p
contains the address of age
, it is necessarily the case that *p
and age
designate the same object. That's what unary *
does. You could also say that for any identifier x
that designates an object, *&x == x
.
Case 2: age == **q
For this I drew a diagram.
I had *q stores address of **q and **q stores address of variable q. So, since variable q = &p and p = & age, so q=& age and thus making the statement age==*q true
True, and again your logic is essentially correct. You might also argue that, by the same reasoning as in Case 1, it is true that *q == p
. From case 1, you know that *p == age
, so we can combine those to say that *(*q) == age
, which is the same as **q == age
.
Case 3: age == *q
This is false but I'm not sure why. *q stores address of [p] which is the same as &p [...]
So far so good.
[...] and since age is same as p
STOP! Flag on the play! age
is not the same as p
. It is the same as *p
, which is quite different.
Suppose I write the street address of my house on a piece of paper. Is the paper the same thing as the house? Of course not. In the same way, if I store the address of an object in a pointer variable, neither the pointer variable nor the pointer value stored in it is the same thing as the object to which the value points.
Case 4: p ==*q
True. This the same as case 1.
Upvotes: 2
Reputation: 61
For case 4, you are trying to compare the value of age against the address of age.
Should be &age == *q or age==**q
You can try to print the value of the pointer against the address of age and you might understand further.
Upvotes: 0