Search code examples
cpointersvoid-pointers

Pointer modification


How to go to a location with pointer addition and change the pointer at that position to point to a new address?
The following code is just an example, I do not want an easy way as just do ptr[1] = (new addr)
but to change the address with the following method:

  1. create a new pointer variable change_ptr
  2. go to designated address with pointer addition
  3. change the pointer on that address to point to new address
int *ptr[5]; 
void *change_addr = ptr[0];
void *p = change_ptr + sizeof(int*);  
*p = (void*)(uintptr_t)(new address);

Solution

  • found out i just need to just void ** and dereference it to change address

    int *ptr[5]; 
    void *change_addr = ptr[0];
    void **p = (void**)( change_ptr + sizeof(int*) );  
    *p = (void*)(uintptr_t)(new address);