Let's say I have a pointer to an integer
int* iPtr;
Given that it already points to a valid memory address, if I want to change the value of it I need to first derrefer the pointer
*iPtr = 100; //atribuo o valor 100
So this *
is what indicates that I'm derreferencing this pointer and assigning the value to the memory it points to, if I did not have *
there I would be trying to change the address this pointer points to
But this works because the compiler knows the type, when you use void*
you have a generic pointer, since you do not have a defined type you can not simply derrefer this pointer, so you first need to cast it to a another type of pointer to be able to derrefer it, considering that we have a void* voidPtr
variable, in the example
*(int*)voidPtr = 100;
First the (int*)
cast void*
to int*
, then the first *
derreferences the pointer to be able to assign the value