warning: pointer of type ‘void *’ used in arithmetic

user1876942 picture user1876942 · Nov 5, 2014 · Viewed 25.8k times · Source

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]

How can I change my code to remove the warnings? I am using C++ and Linux.

Answer

Sean picture Sean · Nov 5, 2014

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.

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;