Pointer Expressions
Pointers are valid operands in arithmetic expressions, assignment expressions and comparison expressions. However, not all the operators normally used in these expressions are valid with pointer variables. This section describes the operators that can have pointers as operands and how these operators are used with pointers.
Several arithmetic operations may be performed on pointers. A pointer may be incremented (++) or decremented (--), an integer may be added to a pointer (+ or +=), an integer may be subtracted from a pointer (- or -=) or one pointer may be subtracted from another.
Assume that array int v[ 5 ] has been declared and that its first element is at memory location 3000. Assume that pointer vPtr has been initialized to point to v[ 0 ] (i.e., the value of vPtr is 3000). Figure 8.18 diagrams this situation for a machine with four-byte integers. Note that vPtr can be initialized to point to array v with either of the following statements (because the name of an array is equivalent to the address of its first element):
int *vPtr = v;
int *vPtr = &v[ 0 ];
Figure 8.18. Array v and a pointer variable vPtr that points to v.
(This item is displayed on page 425 in the print version)
In conventional arithmetic, the addition 3000 + 2 yields the value 3002. This is normally not the case with pointer arithmetic. When an integer is added to, or subtracted from, a pointer, the pointer is not simply incremented or decremented by that integer, but by that integer times the size of the object to which the pointer refers. The number of bytes depends on the object's data type. For example, the statement
vPtr += 2;
would produce 3008 (3000 + 2 * 4), assuming that an int is stored in four bytes of memory. In the array v, vPtr would now point to v[ 2 ] (Fig. 8.19). If an integer is stored in two bytes of memory, then the preceding calculation would result in memory location 3004 (3000 + 2 * 2). If the array were of a different data type, the preceding statement would increment the pointer by twice the number of bytes it takes to store an object of that data type. When performing pointer arithmetic on a character array, the results will be consistent with regular arithmetic, because each character is one byte long.
Figure 8.19. Pointer vPtr after pointer arithmetic.
If vPtr had been incremented to 3016, which points to v[4], the statement
vPtr -= 4;
would set vPtr back to 3000the beginning of the array. If a pointer is being incremented or decremented by one, the increment (++) and decrement (--) operators can be used. Each of the statements
++vPtr;
vPtr++;
increments the pointer to point to the next element of the array. Each of the statements
--vPtr;
vPtr--;
decrements the pointer to point to the previous element of the array.
Pointer variables pointing to the same array may be subtracted from one another. For example, if vPtr contains the location 3000 and v2Ptr contains the address 3008, the statement
x = v2Ptr - vPtr;
would assign to x the number of array elements from vPtr to v2Ptrin this case, 2. Pointer arithmetic is meaningless unless performed on a pointer that points to an array. We cannot assume that two variables of the same type are stored contiguously in memory unless they are adjacent elements of an array.