Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why pointer Data Types have 2 different Addresses C++

I got this problem I don’t understand why

when I try this :

int* a = new int{5};
std::cout << a << '\n' << &a;

the output is : a have address and the &a have different address why is that?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

isn’t &a and a supposed to have the same address of 5?

I know that when you write

int b = 6;
std::cout << &b;

this will output the address of 6

but in the first code it has different address so does that mean pointer have 2 address? or what?

Thanks for reading

>Solution :

a have address and the &a have different address why is that?

a is a pointer object by itself and has an address so &a gives a‘s address while printing a gives the pointer value pointing to the dynamically allocated int.

This is the very basics of C++ and is explained in any beginner level C++ book.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading