Lesson 14 of 50
cPointer Arithmetic
10 mins
Course progress28%
What you'll learn
Keep these ideas in mind as you work through the lesson.
Lesson 14 of 50
c10 mins
Keep these ideas in mind as you work through the lesson.
When a pointer points to an element of an array, adding or subtracting integers moves it by that many elements, not bytes.
int arr[5] = {10,20,30,40,50};
int *p = arr; // same as &arr[0]
printf("%d
", *(p+2)); // -> 30
The compiler automatically scales the offset by sizeof(*p). Pointer arithmetic is only defined within the same array (or one past the last element).