pointer offset doesnt work in memset?

c, memset

Solution

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),

int *p = 0x00004
int *q = p+1;
assert(q == 0x00008)

Basically, it's the same as if you used the index of operator:

int *q = &p[1]

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 variable `a` and possibly overwriting existing data for something else.

Problem

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.

Original source