gpt4 book ai didi

c++ - 警告 : pointer of type ‘void *’ used in arithmetic

转载 作者:可可西里 更新时间:2023-11-01 14:54:47 24 4
gpt4 key购买 nike

我正在从内存映射中写入和读取寄存器,如下所示:

//READ
return *((volatile uint32_t *) ( map + offset ));

//WRITE
*((volatile uint32_t *) ( map + offset )) = value;

但是编译器给我这样的警告:

warning: pointer of type ‘void *’ used in arithmetic [-Wpointer-arith]

如何更改我的代码以删除警告?我正在使用 C++ 和 Linux。

最佳答案

因为 void* 是一个指向未知类型的指针,所以你不能对它进行指针运算,因为编译器不知道指向的东西有多大。

最好的办法是将 map 转换为一个字节宽的类型,然后进行算术运算。您可以使用 uint8_t为此:

//READ
return *((volatile uint32_t *) ( ((uint8_t*)map) + offset ));

//WRITE
*((volatile uint32_t *) ( ((uint8_t*)map)+ offset )) = value;

关于c++ - 警告 : pointer of type ‘void *’ used in arithmetic,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26755638/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com