Once a pointer points to an array, it can access any of the array's elements. By adding or subtracting from the pointer's value (using “pointer arithmetic”) you can access any element in the array, just as you can access it with array subscripts.
So in PARRAY.C, just as in POINTER.C, we can use *ptr to access the int value that ptr references. The only difference is now ptr points to an array element instead of a simple variable.
When the for loop in PARRAY.C executes the first time, ptr points to the first element of i_array, which is i_array[0]. The second statement in the loop body,
ptr++;
increments the pointer. Now ptr points to the next element in i_array, which is i_array[1]. Figure 8.5 shows the relationship of ptr and i_array after the first iteration of the for loop in PARRAY.C.
Figures 8.4 and 8.5 illustrate another important fact about pointers. Pointer arithmetic is automatically scaled to the size of the object that a pointer references. As explained above, incrementing ptr with the statement
ptr++;
Summary: Pointer arithmetic is scaled to the size of elements in an array.
moves the pointer forward to the next element in i_array. Since each element of an int array contains two bytes, this operation actually adds 2 to the address stored in ptr, but you don't have to worry about that detail. The compiler knows the size of the elements in the array and adjusts the pointer accordingly.
Incrementing a pointer adds 1 if it points to a char array, 4 if it points to a float array, and so on.
You can also decrement an array pointer. If ptr points to i_array[2], this statement moves the pointer back one element, to i_array[1]:
ptr--;
Although the previous expressions increment and decrement ptr by 1, you can add or subtract any integer value from a pointer. For instance, the following statement moves ptr forward three elements in i_array:
ptr += 3;
Be careful not to overrun the bounds of an array when accessing its elements with a pointer. As noted in Chapter 4, “Data Types,” the C language doesn't check array subscripts. This rule applies equally when you access an array with a pointer, which can potentially reference any address in memory.
WARNING:
The C language does not check array pointer references. If you increment or decrement a pointer past the limits of an array, you can corrupt other parts of your program or cause other unexpected results.
It's your job to make sure an increment or decrement doesn't move a pointer outside the memory where an array is stored. For instance, if you decrement ptr when it points to i_array[0], it will point to whatever happens to be stored in the int-size memory area below the element i_array[0].
Most pointer arithmetic occurs in connection with arrays, where a numerical index has obvious utility. It's not illegal to do pointer arithmetic on nonarray pointers, but such operations normally serve no purpose. For instance, if you increment a pointer to a simple variable, the pointer no longer points to the variable and becomes useless.