Double and single pointer concept

#include

using namespace std;

int main()
{
int a=5;
int *p=&a;
int **q=&p;
cout<<**q<<" “<<*q<<” “<<q<<” "<<&p;

return 0;

}
if I run this code,shouldn’t the value of *q and &p be the same.If not then what should be the value of *q and why?

Hey @armaanbhardwaj23, I have made a diagrammatic explanation for you. It will help you in understanding your question better :grinning:

1 Like

Thanks a lot for this.Although I’m aware of this concept,I was just trying to implement something different:
I’ve taken a variable a whose value is 5,created a pointer p using address of a and then created a double pointer q with address of p.So I just wanted to know what should be the value of *q.
Should it be a garbage value or what and how do we exactly find this value. I know that
q=&p and **q=a,then basically what should be *q?

See , *q will be value of address of variable q I.e., 30 in diagram and your doubt for &p is that it will show address of p variable I.e., 20 in diagram
This might help :grinning:


According to the output of this program *q is not equal to &q

Yes, when did I say that they both will have same value? *q and &q both will have different value @armaanbhardwaj23

I thought according to this *q should be equal to &q.

That’s fine @armaanbhardwaj23, if you still want to know deep about pointer, reference , address, memory allocation. I can give you a reference video to :grinning:

I mean sure yeah!
But like I was randomly checking what should be the value of *q so can you predict and tell me what’s the output of cout<<*q in the program I wrote.

Hey @armaanbhardwaj23 see this carefully


Whereas for video, you can have a look here https://youtu.be/DJ-o7hDAG7E
Happy to help :grinning:

1 Like

okay I got it…Thank you so much

1 Like

Your welcome @armaanbhardwaj23 :grinning:

1 Like