cpointerspointer-arithmetic

unexpected result when adding to pointer


Someone told me this bit of code prints 29. Why is that?

int *a = 17; 
printf("%d", a+3);

Solution

  • Because when you add to a pointer it adds the object size. In this case the object size is 4 (sizeof(int) == 4) -- so 17 + 3 * 4 == 29.