In C++, pointer arithmetic means performing arithmetic operations on pointers. It refers to the operations that are valid to perform on pointers. Following are the arithmetic operations valid on pointers in C++:
We will explore all the operations listed above in detail with examples.
1. Incrementing and Decrementing Pointer in C++
Incrementing or decrementing a pointer will make it refer to the address of the next or previous data in the memory. This process differs from incrementing and decrementing numeric data.
When we increment or decrement a numeric data, its value is incremented or decremented by 1. However, incrementing or decrementing a pointer, instead of increasing or decreasing by 1, the address increases or decreases by 1 multiplied by the size of the data type it is pointing to. (one of the reasons why the pointer declaration requires information about the type of data it is pointing to)
Incrementing a Pointer
Incrementing a pointer will depend on the type of variable address stored in the pointer. For example, If a 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.
ptr++;
++ptr;
Decrementing a Pointer
When we apply a decrement operation on the pointer then the pointer is decremented by 4 or 8 bytes depending upon the machine. For example, If a pointer ptr holds the address 1004 and we decrement the pointer, then the pointer will be decremented by 4 or 8 bytes (size of the integer), and the pointer will now hold the address 1000.
ptr--;
++ptr

Example
C++
#include <iostream>
using namespace std;
int main() {
int n = 27;
// Storing address of n in ptr
int* ptr = &n;
// Print size of int
cout << "Size of int: " << sizeof(int) << endl;
// Print the address stored at ptr
cout << "Before Increment: " << ptr << endl;
// Increment pointer
ptr++;
cout << "After Increment: " << ptr << endl;
// Print the address stored at ptr
cout << "Before Decrement: " << ptr << endl;
// Decrement pointer
ptr--;
cout << "After Decrement: " << ptr;
return 0;
}
OutputSize 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.
2. 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

Example
C++
// CPP program to demonstrate the addition of a constant to
// a pointer
#include <iostream>
using namespace std;
int main()
{
int n = 20;
int* ptr = &n;
cout << "Address stored in ptr: " << ptr << endl;
// Adding the integer value 1 to the pointer ptr
ptr = ptr + 1;
cout << "Adding 1 to ptr: " << ptr << endl;
// Adding the integer value 2 to the pointer ptr
ptr = ptr + 2;
cout << "Adding 2 to ptr: " << ptr;
return 0;
}
OutputAddress stored in ptr: 0x7ffc79d0fcec
Adding 1 to ptr: 0x7ffc79d0fcf0
Adding 2 to ptr: 0x7ffc79d0fcf8
Explanation: We have initialized a variable of integer type and then initialized a pointer ptr of integer type with an address of a variable n. Now, we print the address stored in pointer ptr after that we have added 1 to ptr and then again print the address stored in ptr. Now we can see in the output that the 4 is added to ptr instead of 1. This is because when we add any constant to the pointer, it first multiplies it to the size of the type of pointer which is integer in this case and here it takes 4 bytes. so, it first multiplies 1 with 4 and then adds the (1*4) to the pointer. The same will happen when we add 2, (2*4) will be added to the pointer ptr.
3. 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

Example
C++
#include <iostream>
using namespace std;
int main()
{
int n = 100;
int* ptr = &n;
cout << "Address stored in ptr: " << ptr << endl;
// Subtracting the integer value 1 from pointer ptr
ptr = ptr - 1;
cout << "Subtract 1 from ptr: " << ptr;
return 0;
}
OutputAddress 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
C++
#include <iostream>
using namespace std;
int main() {
int arr[5] = {1, 2, 3, 4, 5};
int* ptr1 = &arr[2];
// Adding 4 to ptr1 and stored in ptr2
int* ptr2 = &arr[4];
// Subtracting ptr2 from ptr1
cout << ptr2 - ptr1;
return 0;
}
5. 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.
Example
C++
#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;
}
OutputBoth 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.
Example
C++
#include <iostream>
using namespace std;
int main() {
int n = 10;
// Assigning null in case we dont use pointer
int* ptr = NULL;
ptr = &n;
// Checking if the pointer is in use or not
if (ptr == NULL) {
cout << "No value is pointed";
}
else {
cout << *ptr;
}
return 0;
}
OutputThe value pointed is 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.
Similar Reads
C++ Programming Language C++ is a computer programming language developed by Bjarne Stroustrup as an extension of the C language. It is known for is fast speed, low level memory management and is often taught as first programming language. It provides:Hands-on application of different programming concepts.Similar syntax to
5 min read
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Object Oriented Programming in C++ Object Oriented Programming - As the name suggests uses objects in programming. Object-oriented programming aims to implement real-world entities like inheritance, hiding, polymorphism, etc. in programming. The main aim of OOP is to bind together the data and the functions that operate on them so th
5 min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
3-Phase Inverter An inverter is a fundamental electrical device designed primarily for the conversion of direct current into alternating current . This versatile device , also known as a variable frequency drive , plays a vital role in a wide range of applications , including variable frequency drives and high power
13 min read
Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and
9 min read
30 OOPs Interview Questions and Answers [2025 Updated] Object-oriented programming, or OOPs, is a programming paradigm that implements the concept of objects in the program. It aims to provide an easier solution to real-world problems by implementing real-world entities such as inheritance, abstraction, polymorphism, etc. in programming. OOPs concept is
15 min read
What is Vacuum Circuit Breaker? A vacuum circuit breaker is a type of breaker that utilizes a vacuum as the medium to extinguish electrical arcs. Within this circuit breaker, there is a vacuum interrupter that houses the stationary and mobile contacts in a permanently sealed enclosure. When the contacts are separated in a high vac
13 min read
Polymorphism in Java Polymorphism in Java is one of the core concepts in object-oriented programming (OOP) that allows objects to behave differently based on their specific class type. The word polymorphism means having many forms, and it comes from the Greek words poly (many) and morph (forms), this means one entity ca
7 min read