+ 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?

14th Aug 2017, 2:45 AM
Vasanth Mani Vasi Venkatram Vasi
Vasanth Mani Vasi Venkatram Vasi - avatar
4 odpowiedzi
+ 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.
14th Aug 2017, 2:52 AM
aklex
aklex - avatar
+ 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;
14th Aug 2017, 3:05 AM
Hatsy Rei
Hatsy Rei - avatar
+ 1
thx everyone
14th Aug 2017, 3:24 AM
Vasanth Mani Vasi Venkatram Vasi
Vasanth Mani Vasi Venkatram Vasi - avatar
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
14th Aug 2017, 3:01 AM
Guilherme Ferreira Carvalho
Guilherme Ferreira Carvalho - avatar