+ 1
I have doubt regarding pointers and dynamic memory.
https://code.sololearn.com/c1BbeNPIDgad/?ref=app In this code , p is a variable right? When I type cout <<p; why does it show the memory location than the value?
4 Respuestas
+ 8
p is a variable, yes, but it's a specific variable, known as a pointer. It's value is a memory address. Hence when you use cout on it, it prints it in hexadecimal form. And when you dereference it, the CPU goes and fetches an integer from that memory address. In this case 5.
+ 11
A pointer is a variable which stores memory address of other variables. You need to tell the program to print the value by pointing to the memory address stored in the pointer instead of printing the memory address.
std::cout << *p;
+ 1
thx everyone
0
putting the * in some VAR means point to the value assigned in the mem address that is at real the value of VAR.
Btw, putting & before shows you the address that holds VAR , besides VAR is a address too