Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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.

like image 771
user1876942 Avatar asked Nov 05 '14 11:11

user1876942


2 Answers

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; 
like image 89
Sean Avatar answered Oct 26 '22 14:10

Sean


Type void is incomplete type. Its size is unknown. So the pointer arithmetic with pointers to void has no sense. You have to cast the pointer to type void to a pointer of some other type for example to pointer to char. Also take into account that you may not assign an object declared with qualifier volatile.

like image 32
Vlad from Moscow Avatar answered Oct 26 '22 15:10

Vlad from Moscow