Why does int pointer ‘++’ increment by 4 rather than 1?

When you increment a T*, it moves sizeof(T) bytes. This is because it doesn’t make sense to move any other value: if I’m pointing at an int that’s 4 bytes in size, for example, what would incrementing less than 4 leave me with? A partial int mixed with some other data: nonsensical.


Consider this in memory:

    [↓      ]
[...|0 1 2 3|0 1 2 3|...]
[...|int    |int    |...]

Which makes more sense when I increment that pointer? This:

            [↓      ]
[...|0 1 2 3|0 1 2 3|...]
[...|int    |int    |...]

Or this:

      [↓      ]
[...|0 1 2 3|0 1 2 3|...]
[...|int    |int    |...]

The last doesn’t actually point an any sort of int. (Technically, then, using that pointer is UB.)

If you really want to move one byte, increment a char*: the size of of char is always one:

int i = 0;
int* p = &i;

char* c = (char*)p;
char x = c[1]; // one byte into an int

†A corollary of this is that you cannot increment void*, because void is an incomplete type.

Leave a Comment