如何在变量中存储64位内存地址?

fou*_*_29 3 c pointers

我需要在变量中存储64位内存地址,a long应该足够大以容纳该值。

例如:

long arr[2];

//store the memory address of arr[1] in arr[0]
arr[0] = (long)(arr + 1);

printf("arr[0]: 0x%012x Actual address: %p\n", arr[0], (void *)arr);
Run Code Online (Sandbox Code Playgroud)

输出:

arr[0]: 0x00007c6a2c20, Actual address: 0x7ffe7c6a2c20
Run Code Online (Sandbox Code Playgroud)

存储在中的值arr[0]似乎被截断了。

我在这里想念什么?

Sou*_*osh 5

如果要存储64位值,则可以使用uint64_t变量类型。

但是,由于要存储的值是地址,因此可能需要使用uintptr_t,确保平台中该类型的可用大小可以使用。