This is actually quite easy to debug to see what is going on here. Run this piece of code and look at the values:
#include<stdio.h>
int main()
{
int const* a=2;
printf("Size of int %d\n", sizeof(int));
printf("Size of int const* %d\n", sizeof(int const*));
printf("Size of a %d\n", sizeof(a));
printf("Not incremented %d\n", a);
printf("Incremented %d\n",++(a));
return 0;
}
I get the output:
Size of int 4
Size of int const* 8
Size of a 8
Not incremented 2
Incremented 6
On a 64-bit machine. Clearly this demonstrates that when you do '++(a)' you are adding 2 to the size of an int. Why should this be the case when you declared
int const* a=2;
?
Well, that is because you are adding 2 to the size of the pointer type rather than the size of the pointer. Change to:
long const* a = 2;
To observe the differences and see this is the case.
This is a quirk of the C/C++ language.