We know a pointer has a name and a value, the essence is variable, the value is hexadecimal number which is a memory address.
int a = 10;
int * p = &a;
cout << p << endl; // 0x7ffee04fe2b8
int * q = 0x7ffee04fe2b8; // why can not assign address number to the pointer `q`?
If I assign 0x0
, it works:
int * q = 0x0;
The 0x0
refer to NULL, whether because of the interpreter take special handling.
I use CLion editor.
EDIT
I tried to declare as long
, got issue too:
EDIT
I use 64-bit macOS.
The pointer value stores a memory address, so I want to try to assign the address directly. Why can I not use this method?
int a = 10;
int * p = &a;
cout << p << endl; // 0x7ffee04fe2b8
long q = 0x7ffee04fe2b8;
cout << * (int *)q << endl; // prints nothing
EDIT
I tried this post: Pointer to a specific fixed address:
int a = 10;
int * p = &a;
cout << p << endl; // 0x7ffee04fe2b8
volatile unsigned int *myPointer = (volatile unsigned int *)0x7ffee04fe2b8;
cout << *myPointer << endl; // print nothing
But why does it not print anything?
It builds successful, but when I run it, it prints nothing with *myPointer
.
====================[ Build | untitled | Debug ]================================ /Applications/CLion.app/Contents/bin/cmake/mac/bin/cmake --build /Users/luowensheng/CLionProjects/untitled/cmake-build-debug --target untitled -- -j 4 [100%] Built target untitled Build finished