C++ - Pointer Arithmetic

A pointer contains the address of a variable, which is a numeric value. C++ allows few arithmetic operations on a pointer which are slightly different from regular mathematical operations. It supports four arithmetic operators on a pointer which are: ++, --, +, -.

Increment/Decrement of a Pointer

Increment: When a pointer is incremented, it starts pointing to the immediate next location. This is somewhat different from regular mathematical operations since the value of the pointer gets increased by the size of the data type it is pointing to. For Example: let us consider that ptr is an integer pointer which points to the address 1000. When it is incremented, its value gets increased by 4 (size of 64-bit int) and it will point to new address 1004.

Decrement: Similarly, When a pointer is decremented, it starts pointing to the immediate previous location and the value of the pointer gets decreased by the size of the data type it is pointing to. For Example: let us consider that ptr is an integer pointer which points to the address 1000. When it is decremented, its value gets decreased by 4 (size of 64-bit int) and it will point to new address 996.

Example:

The example below illustrates on increment/decrement of a pointer.

#include <iostream>
using namespace std;

int main (){
int Var = 10;
int *p1, *p2;

p1 = p2 = &Var;

cout<<"p1 before incrementing: "<<p1<<"\n";
//incrementing p1
p1++;
cout<<"p1 after incrementing: "<<p1<<"\n";

cout<<"\np2 before decrementing: "<<p2<<"\n";
//decrementing p2
p2--;
cout<<"p2 after decrementing: "<<p2<<"\n";
return 0;
}

The output of the above code will be:

p1 before incrementing: 0x7ffe41f16794
p1 after incrementing: 0x7ffe41f16798

p2 before decrementing: 0x7ffe41f16794
p2 after decrementing: 0x7ffe41f16790

Addition/Subtraction of an integer to a Pointer

Addition: When an integer n is added to a pointer, n is first multiplied by the data type the pointer is pointing to, then it is added to the pointer.

Subtraction: Similarly, When an integer n is subtracted from a pointer, n is first multiplied by the data type the pointer is pointing to, then it is subtracted from the pointer.

Example:

The example below illustrates on addition/subtraction of an integer to a pointer.

#include <iostream>
using namespace std;

int main (){
int Var = 10;
int *p1, *p2;

p1 = p2 = &Var;

cout<<"p1 : "<<p1<<"\n";
p1 = p1 + 5;
cout<<"(p1 + 5) : "<<p1<<"\n";

cout<<"\np2 : "<<p2<<"\n";
//subtracting 5 from p2
p2 = p2 - 5;
cout<<"(p2 - 5) : "<<p2<<"\n";
return 0;
}

The output of the above code will be:

p1 : 0x7ffeb6a6ae44
(p1 + 5) : 0x7ffeb6a6ae58

p2 : 0x7ffeb6a6ae44
(p2 - 5) : 0x7ffeb6a6ae30

Pointer Arithmetic on Arrays

By using the above discussed operations, a pointer can be used to iterate over an array.

Example:

Consider the example below where pointer is used to iterate over an array.

#include <iostream>
using namespace std;

int main (){
int Arr = {10, 20, 30, 40, 50};
int *p1, *p2;

p1 = Arr;      //pointing to Arr
p2 = Arr + 4;  //pointing to Arr

cout<<"Arr contains: ";
//incrementing p1 to iterate in forward direction
for(int i = 0; i < 5; i++) {
cout<<*p1<<" ";
p1++;
}

cout<<"\nArr contains: ";
//decrementing p2 to iterate in backward direction
for(int i = 0; i < 5; i++) {
cout<<*p2<<" ";
p2--;
}
return 0;
}

The output of the above code will be:

Arr contains: 10 20 30 40 50
Arr contains: 50 40 30 20 10

Subtracting two Pointers

Subtracting two pointers is a valid operation although adding two pointers is not a valid operation. The subtraction of two pointers is possible only when they have the same data type. The subtraction of two pointers returns the increments between the two pointers.

Example:

The example below illustrates the subtraction of two int type pointers.

#include <iostream>
using namespace std;

int main (){
int Var = 10;
int *p1, *p2;

p1 = &Var;
p2 = p1 + 5;

cout<<"(p2 - p1) : "<<(p2-p1);
return 0;
}

The output of the above code will be:

(p2 - p1) : 5

Pointer Comparison

Pointers can be compared using relational operators, such as ==, <, >, <= and >=. If two pointers are related to each other such as pointing to the elements of same array, then they can be compared meaningfully.

Example:

In the example below, pointer comparison property is used to iterate over an array. A pointer pointing to an element of an array is incremented in each loop until its value becomes greater than the address of the last element of the array.

#include <iostream>
using namespace std;

int main (){
int Arr = {10, 20, 30, 40, 50};
int *p1;

p1 = Arr;   //pointing to Arr

while(p1 <= &Arr) {
cout<<"p1 : "<<p1<<"\n";
cout<<"*p1 : "<<*p1<<"\n";
p1++;
}
return 0;
}

The output of the above code will be:

p1 : 0x7fff25dae7f0
*p1 : 10
p1 : 0x7fff25dae7f4
*p1 : 20
p1 : 0x7fff25dae7f8
*p1 : 30
p1 : 0x7fff25dae7fc
*p1 : 40
p1 : 0x7fff25dae800
*p1 : 50

❮ C++ - Pointers

5