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?

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.

Leave a Reply