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
Easy example: treat the memory as an array of unsigned char
void *memory = malloc(1000); //allocate a pool of memory
uint8_t *ptr = memory+10;
*ptr = 1 //set an integer value at byte 10
uint8_t i = *ptr; //read an integer value from the 10th byte
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*.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With