1

我正在尝试将地址映射存储在数组中。

以下代码片段在我的 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_tint32_t作为addressMap数组的数据类型。同样的警告。

根据本文档(http://www.keil.com/support/man/docs/armcc/armcc_chr1359125009502.htm)地址为 32 位宽(如预期)。

如果没有此警告,我该如何编写代码?

4

1 回答 1

3

如果没有此警告,我该如何编写代码?

正如警告所说,只需添加演员表

int* c = (int*) addressMap[0];

避免警告initialization makes pointer from integer without a cast [-Wint-conversion]

intptr_t但是,如果addressMapint*的目标是包含指向int的指针,我建议您不要直接使用,因为您根本不需要强制转换:

int * addressMap[2];

int* a=NULL;
int* b=NULL;

*a=10;
*b=20;

addressMap[0] = a;
addressMap[1] = b;

int* c = addressMap[0];
于 2019-04-24T21:34:18.760 回答