Arithmetic Operations on Pointers in C
A C pointer is an address represented by a numeric value. Therefore, you can perform arithmetic operations on pointers. There are four arithmetic operations that can be performed on pointers: ++, --, +, and -.
Suppose ptr is an integer pointer pointing to address 1000, which is a 32-bit integer. Let's perform the following arithmetic operations on this pointer:
ptr++
After executing the above operation, ptr will point to position 1004 because each increment of ptr moves it to the next integer position, which is 4 bytes ahead of the current position. This operation moves the pointer to the next memory location without affecting the actual values in the memory locations. If ptr points to a character at address 1000, the above operation would cause the pointer to point to position 1001, because the next character position is at 1001.
To summarize:
- Each increment of a pointer moves it to the next storage location for an element.
- Each decrement of a pointer moves it to the previous storage location for an element.
- The number of bytes jumped during increment and decrement depends on the data type length of the variable pointed to, such as 4 bytes for an int.
Incrementing a Pointer
We prefer to use pointers in our programs instead of arrays because a pointer variable can be incremented, whereas an array name cannot be incremented since it is a constant pointer. The following program increments the pointer variable to sequentially access each element in the array:
Example
#include <stdio.h>
const int MAX = 3;
int main ()
{
int var[] = {10, 100, 200};
int i, *ptr;
/* Array address in pointer */
ptr = var;
for ( i = 0; i < MAX; i++)
{
printf("Address: var[%d] = %p\n", i, ptr );
printf("Value: var[%d] = %d\n", i, *ptr );
/* Move to the next position */
ptr++;
}
return 0;
}
When the above code is compiled and executed, it produces the following result:
Address: var[0] = e4a298cc
Value: var[0] = 10
Address: var[1] = e4a298d0
Value: var[1] = 100
Address: var[2] = e4a298d4
Value: var[2] = 200
Decrementing a Pointer
Similarly, a pointer can be decremented by subtracting the value of its data type's byte size, as shown below:
Example
#include <stdio.h>
const int MAX = 3;
int main ()
{
int var[] = {10, 100, 200};
int i, *ptr;
/* Address of the last element in pointer */
ptr = &var[MAX-1];
for ( i = MAX; i > 0; i--)
{
printf("Address: var[%d] = %p\n", i-1, ptr );
printf("Value: var[%d] = %d\n", i-1, *ptr );
/* Move to the previous position */
ptr--;
}
return 0;
}
When the above code is compiled and executed, it produces the following result:
Address: var[2] = 518a0ae4
Value: var[2] = 200
Address: var[1] = 518a0ae0
Value: var[1] = 100
Address: var[0] = 518a0adc
Value: var[0] = 10
Pointer Comparison
Pointers can be compared using relational operators such as ==, <, and >. If p1 and p2 point to related variables, such as different elements of the same array, you can compare p1 and p2.
The following program modifies the above example to increment the pointer variable as long as it points to an address less than or equal to the address of the last element of the array &var[MAX - 1]:
Example
#include <stdio.h>
const int MAX = 3;
int main ()
{
int var[] = {10, 100, 200};
int i, *ptr;
/* Address of the first element in pointer */
ptr = var;
i = 0;
while ( ptr <= &var[MAX - 1] )
{
printf("Address: var[%d] = %p\n", i, ptr );
printf("Value: var[%d] = %d\n", i, *ptr );
/* Move to the next position */
ptr++;
i++;
}
return 0;
}
When the above code is compiled and executed, it produces the following result:
Address: var[0] = 0x7ffeee2368cc
Value: var[0] = 10
Address: var[1] = 0x7ffeee2368d0
Value: var[1] = 100
Address: var[2] = 0x7ffeee2368d4
Value: var[2] = 200