警告:算术中使用的类型为"void *"的指针 [英] warning: pointer of type ‘void *’ used in arithmetic
本文介绍了警告:算术中使用的类型为"void *"的指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在从内存映射中读写寄存器,如下所示:
I am writing and reading registers from a memory map, like this:
//READ
return *((volatile uint32_t *) ( map + offset ));
//WRITE
*((volatile uint32_t *) ( map + offset )) = value;
但是编译器给我这样的警告:
However the compiler gives me warnings like this:
warning: pointer of type ‘void *’ used in arithmetic [-Wpointer-arith]
如何更改代码以删除警告?我正在使用C ++和Linux.
How can I change my code to remove the warnings? I am using C++ and Linux.
推荐答案
由于void*
是指向未知类型的指针,因此您无法对其进行指针算术运算,因为编译器将不知道所指向的内容有多大到是.
Since void*
is a pointer to an unknown type you can't do pointer arithmetic on it, as the compiler wouldn't know how big the thing pointed to is.
您最好的选择是将map
强制转换为一个字节宽的类型,然后进行算术运算.您可以为此使用 uint8_t
:
Your best bet is to cast map
to a type that is a byte wide and then do the arithmetic. You can use uint8_t
for this:
//READ
return *((volatile uint32_t *) ( ((uint8_t*)map) + offset ));
//WRITE
*((volatile uint32_t *) ( ((uint8_t*)map)+ offset )) = value;
这篇关于警告:算术中使用的类型为"void *"的指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文