In C++, pointer arithmetic means performing valid arithmetic operations on pointer variables to move and access memory locations efficiently.
- Incrementing a Pointer : The value of pointer is incremented depending on the type of variable address stored in the pointer. For example, If an integer pointer ptr holds the address 1000 and we increment the pointer, then the pointer will be incremented by 4 or 8 bytes (size of the integer), and the pointer will now hold the address 1004 or 1008.
- Decrementing a Pointer : Like increment, the value is decreased according to the size of the type.
The following diagram assumes size of integer as 4 bytes.

#include <iostream>
using namespace std;
int main() {
int n = 27;
int* ptr = &n;
cout << "Size of int: " << sizeof(int) << endl;
cout << "Before Increment: " << ptr << endl;
ptr++;
cout << "After Increment: " << ptr << endl;
cout << "Before Decrement: " << ptr << endl;
ptr--;
cout << "After Decrement: " << ptr;
return 0;
}
Output
Size of int: 4 Before Increment: 0x7ffcbc721cec After Increment: 0x7ffcbc721cf0 Before Decrement: 0x7ffcbc721cf0 After Decrement: 0x7ffcbc721cec
Explanation: We have subtracted '1' from pointer ptr which stored the address of variable 'n'. While subtraction firstly '1' is multiplied by the size of the integer which is 4 bytes and then subtracted from the pointer. In the same way, this will be applicable to other data types such as float, double, char, etc.
Addition of Constant to Pointers
We can add integer values to Pointers and the pointer is adjusted based on the size of the data type it points to. For example, if an integer pointer ptr stores the address 1000 and we add the value 5 to the pointer.
ptr + 5
It will calculate the new address as:
1000 + (5 * 4(size of an integer)) = 1020

#include <iostream>
using namespace std;
int main(){
int n = 20;
int* ptr = &n;
cout << "Address stored in ptr: " << ptr << endl;
ptr = ptr + 1;
cout << "Adding 1 to ptr: " << ptr << endl;
ptr = ptr + 2;
cout << "Adding 2 to ptr: " << ptr;
return 0;
}
Output
Address stored in ptr: 0x7ffc79d0fcec Adding 1 to ptr: 0x7ffc79d0fcf0 Adding 2 to ptr: 0x7ffc79d0fcf8
Explanation:
- An integer variable n is created and a pointer ptr stores its address.
- The program first prints the address stored in ptr.
- When 1 is added to ptr, the address increases by 4 bytes, not by 1.
- This happens because ptr points to an int, and an int occupies 4 bytes.
- So adding 1 to ptr actually adds 1 × 4 = 4 bytes to its address.
Subtraction of Constant from Pointers
We can also subtract a constant from Pointers and it is the same as the addition of a constant to a pointer. For example, if an integer pointer ptr stores the address 1000 and we subtract the value 5 from the pointer:
ptr - 5
It will calculate the new address as:
1000 - (5 * 4(size of an integer)) = 980

#include <iostream>
using namespace std;
int main(){
int n = 100;
int* ptr = &n;
cout << "Address stored in ptr: " << ptr << endl;
ptr = ptr - 1;
cout << "Subtract 1 from ptr: " << ptr;
return 0;
}
Output
Address stored in ptr: 0x7ffdd556d7cc Subtract 1 from ptr: 0x7ffdd556d7c8
4. Subtraction of Two Pointers of the Same Datatype
The Subtraction of two pointers can be done only when both pointers are of the same data type. The subtraction of two pointers gives the number of elements present between the two memory addresses so it is primarily valid if the two pointers belong to the same array.
For Example, in an array arr[10], ptr1 point points to the element at index 2 and ptr2 points to the element at index 4, then the difference between two pointers will give you the number of elements between them.
ptr2 - ptr1
Example
#include <iostream>
using namespace std;
int main() {
int arr[5] = {1, 2, 3, 4, 5};
int* ptr1 = &arr[2];
int* ptr2 = &arr[4];
cout << ptr2 - ptr1;
return 0;
}
Output
2
Comparison of Pointers
In C++, we can perform a comparison between the two pointers using the relational operators(>, <, >=, <=, ==, !=). We generally use this operation to check whether the two-pointer as pointing to the same memory location or not.
#include <iostream>
using namespace std;
int main() {
int n = 10;
int* ptr1 = &n;
int** ptr2 = &ptr1;
int* ptr3 = *ptr2;
// comparing equality
if (ptr1 == ptr3) {
cout << "Both point to same memory location";
}
else {
cout << "ptr1 points to: " << ptr1 << endl;
cout << "ptr3 points to: " << ptr3;
}
return 0;
}
Output
Both point to same memory location
Explanation:
- An integer variable "n" is created with value 10.
- "ptr1" stores the address of "n".
- "ptr2" stores the address of "ptr1", and "ptr3" gets the value stored in "ptr2", which is again the address of n.
- So both "ptr1" and "ptr3" point to the same memory location of n.
- Therefore, the condition "ptr1" == "ptr3" becomes true and the program prints "Both point to same memory location".
Comparison to NULL
We can compare the pointer of a type to NULL. This operation helps us to find whether the given pointer points to some memory address or not. It helps us to control errors such as segmentation faults.
#include <iostream>
using namespace std;
int main() {
int n = 10;
int* ptr = NULL;
ptr = &n;
if (ptr == NULL) {
cout << "No value is pointed";
}
else {
cout << *ptr;
}
return 0;
}
Output
10
Note: It is recommended that we should initialize new pointer variables with NULL so that we can make check if the pointer is assigned some meaningful value.