If p
is a pointer to type T
, and it holds memory address X
, then p + N
is the memory address X + N * sizeof(T)
.
(int) (a + 1) == 0 + 1 * sizeof(char) == 1 * 1 == 1
(int) (b + 1) == 0 + 1 * sizeof(int) == 1 * 4 == 4
(int) (c + 1) == 0 + 1 * sizeof(double) == 1 * 8 == 8
(int) (a + 5) == 0 + 5 * sizeof(char) == 5 * 1 == 5
(int) (b + 7) == 0 + 7 * sizeof(int) == 7 * 4 == 28
(int) (c + 17) == 0 + 17 * sizeof(double) == 17 * 8 == 136
Minor note: As Barmar points out in his answer, arithmetic on NULL pointers is technically undefined behavior. So a standard-compliant compiler could have done something different with that arithmetic; the code might have printed 1, 3.1415, or a picture of dogs playing poker. But the behavior you observed is the same as the defined behavior you would've observed if you'd started with valid pointers, so you probably don't need to worry about this.