C++ Pointer Arithmetic
A pointer is an address represented by a numeric value. Therefore, you can perform arithmetic operations on pointers. There are four arithmetic operations you can perform on pointers: ++, --, +, and -.
Assume 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 moves the pointer 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. 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.
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 an array:
Example
#include <iostream>
using namespace std;
const int MAX = 3;
int main ()
{
int var[MAX] = {10, 100, 200};
int *ptr;
// Array address in pointer
ptr = var;
for (int i = 0; i < MAX; i++)
{
cout << "Address of var[" << i << "] = ";
cout << ptr << endl;
cout << "Value of var[" << i << "] = ";
cout << *ptr << endl;
// Move to the next position
ptr++;
}
return 0;
}
When the above code is compiled and executed, it produces the following result:
Address of var[0] = 0xbfa088b0
Value of var[0] = 10
Address of var[1] = 0xbfa088b4
Value of var[1] = 100
Address of var[2] = 0xbfa088b8
Value of var[2] = 200
Decrementing a Pointer
Similarly, you can decrement a pointer, which means reducing its value by the number of bytes of its data type, as shown below:
Example
#include <iostream>
using namespace std;
const int MAX = 3;
int main ()
{
int var[MAX] = {10, 100, 200};
int *ptr;
// Address of the last element in pointer
ptr = &var[MAX-1];
for (int i = MAX; i > 0; i--)
{
cout << "Address of var[" << i << "] = ";
cout << ptr << endl;
cout << "Value of var[" << i << "] = ";
cout << *ptr << endl;
// Move to the next position
ptr--;
}
return 0;
}
When the above code is compiled and executed, it produces the following result:
Address of var[3] = 0xbfdb70f8
Value of var[3] = 200
Address of var[2] = 0xbfdb70f4
Value of var[2] = 100
Address of var[1] = 0xbfdb70f0
Value of var[1] = 10
Pointer Comparison
Pointers can be compared using relational operators such as ==, <, and >. If p1 and p2 point to related variables, such as elements of the same array, you can compare the sizes of p1 and p2.
The following program modifies the above example, incrementing the pointer variable as long as the address it points to is less than or equal to the address of the last element of the array, &var[MAX - 1]:
Example
#include <iostream>
using namespace std;
const int MAX = 3;
int main ()
{
int var[MAX] = {10, 100, 200};
int *ptr;
// First element address in pointer
ptr = var;
int i = 0;
while ( ptr <= &var[MAX - 1] )
{
cout << "Address of var[" << i << "] = ";
cout << ptr << endl;
cout << "Value of var[" << i << "] = ";
cout << *ptr << endl;
// Move to the next position
ptr++;
i++;
}
return 0;
}
cout << "Address of var[" << i << "] = ";
cout << ptr << endl;
cout << "Value of var[" << i << "] = ";
cout << *ptr << endl;
// Move to the previous position
ptr++;
i++;
}
return 0;
}
When the above code is compiled and executed, it produces the following result:
Address of var[0] = 0xbfce42d0
Value of var[0] = 10
Address of var[1] = 0xbfce42d4
Value of var[1] = 100
Address of var[2] = 0xbfce42d8
Value of var[2] = 200