copy to void* pointer
10,438
If you are certain that the memory referenced by the void pointer is an int, then you can be confident in
*(int *)data = r;
However a better general solution is:
memcpy(data, &r, sizeof(int));
This way you don't have to worry about byte alignment or other potential gotchas.
Author by
YAKOVM
Updated on June 04, 2022Comments
-
YAKOVM almost 2 years
I have a function
getData(void* data)
which should copy to data some internal calculated values for exampleint r = getRadius();
r should be copied to data ,and is such a way returned from function getData what is the correct way to do it? I tried*(int*)data = r;
but I am not sure this is a best solution.