我正在尝试在数组中存储地址映射。
以下代码片段在我的STM32F767ZI上按预期工作,但编译时出现警告...
intptr_t addressMap[2];
int* a=NULL;
int* b=NULL;
*a=10;
*b=20;
addressMap[0]=(intptr_t) a;
addressMap[1]=(intptr_t) b;
int* c=addressMap[0];
编译警告:
initialization makes pointer from integer without a cast [-Wint-conversion]
在最后一行(int* c=addressMap[0];
)。
我还尝试了uint32_t
和int32_t
作为addressMap
数组的数据类型。同样的警告。
根据该文档(http://www.keil.com/support/man/docs/armcc/armcc_chr1359125009502.htm)地址是32位宽(正如预期的那样)。
如果没有这个警告我怎么写我的代码?
如果没有这个警告我怎么写我的代码?
正如警告所说,只需添加一个演员
int* c = (int*) addressMap[0];
避免警告initialization makes pointer from integer without a cast [-Wint-conversion]
但是,如果addressMap的目标是包含指向int的指针,我建议你不要使用intptr_t
但是直接使用int*
,这要归功于你根本不需要转换:
int * addressMap[2];
int* a=NULL;
int* b=NULL;
*a=10;
*b=20;
addressMap[0] = a;
addressMap[1] = b;
int* c = addressMap[0];