Plain C, on Windows 7 & HP machine.
int main(void) {
unsigned int a = 4294967295;
unsigned int *b = &a;
printf("before val: '%u'\n", *b); // expect 4294967295, got 4294967295
memset(b+2, 0, 1);
printf("after val: '%u'\n", *b);
// little endian 4th 3rd 2nd 1st
// expect 4278255615 - 11111111 00000000 11111111 11111111
// got 4294967295 - 11111111 11111111 11111111 11111111
return 0;
}
I want to set the third byte of the integer to 0x0, but is remains the same. Any ideas? Thank you.
On my machine, int is 32 bits.
Pointer addition/subtraction does not move by only one byte – it moves by the size of the type of the object being pointed to.
That is to say (assuming 4-byte integers),
Basically, it’s the same as if you used the index of operator:
If you want to increment a pointer by one, cast it to a
unsigned char *. The way you did it, you were overwriting memory that was not part of the variableaand possibly overwriting existing data for something else.