目前我在预处理器中遇到了一个非常奇怪的错误。我已经搜索过但找不到任何相关内容,因此想将其张贴在这里。 下面是我的示例代码
#include <stdio.h>
#define DDR_BASEADDR 0x000000000U + 128*1024*1024
int main()
{
uint32_t* test2 = (uint32_t*) DDR_BASEADDR;
uint32_t whatIsThis = DDR_BASEADDR;
uint32_t* test3 = (uint32_t*) whatIsThis;
printf( "%x %x %x %x\n\r", DDR_BASEADDR, test2, test3, whatIsThis);
return 0;
}
这段代码的输出应该都是0x8000000
。
然而,输出是:8000000 20000000 8000000 8000000
。
我相信这不是导致此问题的数据类型,因为即使我将 uint32_t* test2 = (uint32_t*) DDR_BASEADDR;
更改为 int32_t* test2 = (int32_t *) DDR_BASEADDR;
我在 Zedboard 的 ARM A9 和 C++ online compiler 上进行了测试并得到相同的结果。 感谢您投入时间和精力。
陈升
最佳答案
宏扩展给你
uint32_t* test2 = (uint32_t*) 0x000000000U + 128*1024*1024;
当你可能期望的时候
uint32_t* test2 = (uint32_t*) (0x000000000U + 128*1024*1024);
这就是为什么建议在宏定义中使用大量括号,或者 - 甚至更好 - 使用函数。
关于c++ - 预处理器的奇怪错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44745306/