How do I make something like this work?
void *memory = malloc(1000); //allocate a pool of memory
*(memory+10) = 1; //set an integer value at byte 10
int i = *(memory+10); //read an integer value from the 10th byte
How do I make something like this work?
void *memory = malloc(1000); //allocate a pool of memory
*(memory+10) = 1; //set an integer value at byte 10
int i = *(memory+10); //read an integer value from the 10th byte
So, by "work" I assume you mean "how do I dereference/perform pointer arithmetic on a
void*
"? You can't; you have to cast it, typically to achar*
if you're just concerned with reading chunks of memory. Of course, if that's the case, simply declare it as achar*
to begin with.Easy example: treat the memory as an array of unsigned char
You can use integers too, but then you must pay attention about the amount of bytes you are setting at once.
The rules are simple:
From this you can conclude that if you want to perform "raw" pointer arithmetic you have to cast to and from char*.