Adding an integer to a pointer moves by sizeof(element) bytes, not one byte. This enables efficient array iteration without index syntax.
Walking an array
int data[] = {1, 2, 3};
int *p = data;
for (int i = 0; i < 3; i++) {
printf("%d\n", *(p + i));
}
Pointer differences
end - start yields the number of elements between two pointers into the same array—undefined if they do not point into the same object.
Important interview questions and answers
- Q: p + 1 for int*?
A: Advances bysizeof(int)bytes to the next int, not the next byte. - Q: Arithmetic on void*?
A: Not allowed in standard C—cast to a typed pointer first.
Self-check
- Are p[i] and *(p+i) equivalent?
- Why must pointer subtraction stay within one array?
Interview prep
- p + 1 for int*?
Advances by
sizeof(int)bytes, not one byte—scale depends on pointed-to type.