cout p and &p gives two different memory addresses, why? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

cout p and &p gives two different memory addresses, why?

Using this example code from dynamic memory section if I cout both p and &p I get two different memory addresses, can someone tell me why that might be? Thanks :) int *p = new int; *p = 5;

9th Apr 2017, 2:27 AM
Eric Williams
Eric Williams - avatar
2 Answers
+ 13
When you cout p, you get the address p is storing (so in this case, wherever the 5 is it's pointing to). When you cout &p, you see the address of where p is stored.
9th Apr 2017, 2:45 AM
Tamra
Tamra - avatar
+ 9
As a pointer, p stores the address of other variables. This is shown by printing the address stored inside the pointer p. The pointer itself, has memory allocated to it. &p would return the address of the pointer itself.
9th Apr 2017, 3:19 AM
Hatsy Rei
Hatsy Rei - avatar