-2

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

image


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:

image


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
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
aircraft
  • 25,146
  • 28
  • 91
  • 166
  • Comments are not for extended discussion; this conversation has been [moved to chat](https://chat.stackoverflow.com/rooms/218491/discussion-on-question-by-aircraft-why-can-not-assign-memory-address-number-to-c). – Samuel Liew Jul 24 '20 at 03:59
  • The value of a pointer is **not** a hexadecimal number. It's just **displayed** that way. Values aren't decimal, octal, hexadecimal, or whatever. When you convert them to text in order to look at them the text has a base. – Pete Becker Jul 24 '20 at 13:07

2 Answers2

2

In the C language you CAN assign addresses from constants. It is not a good idea. But you can do it.

Example compiler error:

$ cat c-ptr-test.c
int main() {
  long *p = 0x0011223344556677;
  return 0;
}
$ make c-ptr-test
cc -Wall -W -pedantic -g -O3 -flto -fno-fat-lto-objects -pthread -MMD  -std=c11   -pthread -flto -fno-fat-lto-objects  c-ptr-test.c   -o c-ptr-test
c-ptr-test.c: In function ‘main’:
c-ptr-test.c:2:13: warning: initialization of ‘long int *’ from ‘long int’ makes pointer from integer without a cast [-Wint-conversion]
    2 |   long *p = 0x0011223344556677;
      |             ^~~~~~~~~~~~~~~~~~
c-ptr-test.c:2:9: warning: unused variable ‘p’ [-Wunused-variable]
    2 |   long *p = 0x0011223344556677;
      |         ^

Two warnings but GCC compiled it successfully.

However, in the C++ language it requires an explicit cast. You're supposed to use reinterpret_cast but an old C style cast will also work.

Zan Lynx
  • 53,022
  • 10
  • 79
  • 131
  • There you go. +1. This is exactly what I mentioned in my comments. Always actually compile your application, as the third-party tool's suggestions are ok, but are not fool-proof. – PaulMcKenzie Jul 24 '20 at 03:49
  • I tried GCC 10.1 9.1 but both shows as [errors](https://godbolt.org/z/svvczf). Is it related to compiler options? – Louis Go Jul 24 '20 at 03:59
  • 2
    @LouisGo Ah, C++. Different language. You're right, C++ won't compile that. Use `reinterpret_cast(0x0011223344556677)` `reinterpret_cast` is also known as "break my program in ways impossible to debug` – Zan Lynx Jul 24 '20 at 04:04
0

It seems you are trying to assign pointer while not telling compiler you "want" to assign it.

Your compiler error should looks like this.

invalid conversion from 'long int' to 'int*'

To assign the addres, the syntax should be int *q = (int*)0x7ffee04fe2b8;,while (int*) means "this value is an address of pointer int*.

Or a more modern way : int * q = reinterpret_cast<int*>( 0x7ffebfc7019c );

However you must to know what are you doing. Unless you're coding low level code with hardware registers, never hardcoded address.

Also everytime you execute the code, its address might not be the same as last time.

Try following code: or on GCC 10.1 https://godbolt.org/z/8zn7j5

int main()
{
   int a = 10;
    int * p = &a;

    cout << p << endl;

    long addr = (long)p;
    int * q = reinterpret_cast<int*>( addr );
    cout << *q << endl;
    return 0;
}
Louis Go
  • 2,213
  • 2
  • 16
  • 29