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

前端 未结 3 2166
南方客
南方客 2021-02-18 23:09

I am writing and reading registers from a memory map, like this:

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

//WRITE
*((volatile uint32_t *) ( map          


        
3条回答
  •  眼角桃花
    2021-02-18 23:18

    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;
    

提交回复
热议问题