简体   繁体   English

用地址值初始化指针

[英]Initialize a pointer with an address value

I need to initialize a pointer variable with a knowing address. 我需要用一个已知的地址来初始化一个指针变量。 Please see code below, ptr is the final destination and value of ptr_address contains the address value, so I need to do something like ptr = value. 请参见下面的代码,ptr是最终的目的地,而ptr_address的值包含地址值,因此我需要执行类似ptr = value的操作。

int *ptr;
int address;

address = 0x10000005;
ptr = address;

The problem is that compiler gives the following warning message: 问题是编译器给出以下警告消息:

warning: assignment makes pointer from integer without a cast [enabled by default]

Is my code wrong or there is any other way to do it without receiving this compiler warning? 我的代码是错误的还是没有收到此编译器警告的其他方法?

Use a cast: 使用演员表:

intptr_t some_variable = 25;

int * ptr1 = (int *) 0x10000005;
int * ptr2 = (int *) some_variable;

Or in C++: 或在C ++中:

int * ptr1 = reinterpret_cast<int *>(0x10000005);
int * ptr2 = reinterpret_cast<int *>(some_variable);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM