Pointer Arithmetic
Pointer Arithmetic serves as another way to traverse arrays. The snippet of
code below demonstrates pointer arithmetic:
int ivar[5], *intPtr;
double dvar[5], *doublePtr;
char cvar[5], *charPtr;
intPtr = ivar;
doublePtr = dvar;
charPtr = cvar;
intPtr = intPtr + 1; /* Adds 4 to the address currently held in
* intPtr. This will cause intPtr to access the
* second element in the array. */
doublePtr++; /* Same concept as above but 8 is added to doublePtr */
charPtr += 1; /* This time 1 is added to charPtr. */
Notice that I assigned the address held in the array's variableName to a new
pointer before changing its value. This is due to the fact that the address
held in the array's variableName cannot be edited unless it is re-assigned into
a new variable.
If you wanted to access the array elements using arithmetic, you could do so by
adding the indice to the array's variableName. This will access that element
of the array, but you still cannot change the address held in the
varialbeName.
Main Page
Last Modified 31 October 2000