Even if you have a constant instead of sizeof(int) , the compiler cannot know the address in p beforehand, so it will have to make an addition. If you have something like i = sizeof(int)+4 , then it should execute compile time with optimizations and directly set i to 8 .
Also, I think when you do this:
*((int*)p+sizeof(int)) = 777;
what do you mean:
*((int*)p + 1) = 777;
Similar to printf("%d ", *((int*)p+sizeof(int)*i)); should be:
printf("%d ", *((int*)p + i));
source share