I’m trying to write a function that takes a void pointer to an int and then doubles the int, and puts it back into the memory location:
void doubleNumber(void *number){
number = &((*((int*)(number))) * 2);
}
So first I cast it into an int * from a void *, then I deference the int * to get the value, then I multiply by 2 and then I get the address of that to put it back into the pointer.
Can anyone give me tips on why my logic is not working?
Thanks
I’d write it like this:
First of all cast
numberto be of typeint*. Then dereference the pointer. Then double it.The problem with your code is that you are assigning the pointer rather than the pointee.