Tag Archives: void*

Pointer arithmetic to void* (uintptr_t)

The link below is a very important one regarding pointer arithmetic, a subject that frequently is being taught incorrectly in many universities:

https://stackoverflow.com/questions/3523145/pointer-arithmetic-for-void-pointer-in-c

The best way for general arithmetic on pointers for most architectures is to use the uintptr_t  data type whenever available.

Cast the pointer to uintptr_t and make any arithmetic operation you want. Then cast the uintptr_t back to the type of the pointer you want:

void calculateModulo(char *address) { 
    uintptr_t x = (uintptr_t)address; 
    x = x % 4;
    char * resultingaddress = (char*) x;
    ....
}