Pointer Arithmetic in C++
Pointer arithmetic in the C++ programming language allows C++ users to perform operations on array data types or element value pointers stored in memory blocks. Pointer arithmetic is an essential concept for working with low-level memory management features in C++, iterating through loops over array block data element values in a continuous sequence, and manipulating program-defined data in the proper order.

So, let’s take a closer look at the key operational features of pointer arithmetic in C++ programming.
Pointer Arithmetic Operation Concepts in C++.
In C++ programming, C++ users can perform several basic operations or conditions with pointer data type variables in a program.
Common Pointer Arithmetic Operations in C++.
- Incrementing a pointer variable element (pntr++)
- Decrementing a pointer parameter value (pntr–)
- Adding an integer value to a pointer (pntr + n)
- Subtracting an integer value from a pointer (pntr – n)
- Subtracting two pointer values (pntr1 – pntr2)
C++ users perform different pointer data behavior operations based on the pointer’s data type (e.g., what data type value element it points to).
So, let’s look at pointer variable operations with some examples.
Incrementing and Decreasing Pointer Data Variables Operations.
When C++ users define a pointer value element in a program, they increment (pntr++) or decrement (pntr–). So, C++ users are modifying the pointer in this condition to point to or increment the next or previous value element of the data type it actually points to.
Element of Incrementing and Decreasing Pointer Data Variables.
- pntr++ – This increments the pointer value in the current program by the size of the data type it points to. (For example, if it is an int*, it moves it by sizeof(int)).
- pntr— – This decrements the pointer value in the current program by the size of the type the variable value points to.
Example of Incrementing and Decreasing Pointer Data Variables.
#include <iostream>
int main() {
int array[] = {89, 39, 82, 90, 11, 44};
int* pntr = array; // here we use pointer to the first element of the array
std::cout << “First array element value is – ” << *pntr << std::endl; // Result is – 89
pntr++; // here it moves to the next array element (pointer points to array[1])
std::cout << “Next array element value is – ” << *pntr << std::endl; // Result is – 39
pntr–; // here it move back to the previous array element (pointer points to array[0])
std::cout << “Previous array element value is – ” << *pntr << std::endl; // Result is – 89
return 0;
}
Explanation of Increasing and Decreasing Pointer Data Variables.
- In this example, pntr++ moves the pointer to the next array element (array[1]).
- Similarly, pntr– moves it to the previous array element (array[0]).
Adding an integer to a pointer operation concept in C++.
C++ users can add an integer n to a pointer program to move a defined value forward by n elements. This pointer will move n positions in data memory, with each step being the size of the data type the current pointer value points to.
Example of adding an integer to a pointer operation.
#include <iostream>
int main() {
int array[] = {89, 39, 55, 61, 11, 44};
int* pntr = array; // Here we define a pointer to the first element of the above array.
std::cout << “The first array element value is – ” << *pntr << std::endl; // Result is – 89
pntr = pntr + 3; // Here it moves the pointer 3 positions forward (to array[3] location)
std::cout << “The fourth array element value is – ” << *pntr << std::endl; // Result is – 61
return 0;
}
Explanation of adding an integer to a pointer operation.
- In this example, pntr = pntr + 3 moves the pointer 3 positions (to the array[3] storage address location).
Subtracting an Integer from a Pointer Operation Concepts in C++.
C++ users can subtract an integer value n from a pointer value defined in a program. This numeric value operation moves the pointer n positions backward in memory.
Example of subtracting an integer from a pointer.
#include <iostream>
int main() {
int array[] = {100, 39, 55, 61, 11, 399};
int* pntr = array + 5; // Here we define a pointer to the last element of the array (array[5])
std::cout << “Last array element value is – ” << *pntr << std::endl; // Result is – 399
pntr = pntr – 1; // Here it moves the pointer 1 position backward (to array[1] location)
std::cout << “Fourth array element value is – ” << *pntr << std::endl; // Result is – 11
return 0;
}
Explanation of subtracting an integer from a pointer.
- In this example, pntr = pntr – 1 moves the pointer backward one position, making it point to the value array[1].
Subtracting two pointers Operation Concepts in C++.
When a C++ user subtracts two pointers in an existing program, the output results in the difference in array element numbers between them, not the difference in array bytes. This operation can be performed in C++ only if both user-defined pointers point to elements of the same array or memory block.
Syntax of subtracting two pointers.
pntrFirst – pntrSecond
In this syntax, pntrFirst and pntrSecond indicate the number of array elements between them. The output result contains numbers of array elements, not numbers of bytes.
Example of subtracting two pointers.
#include <iostream>
int main() {
int array[] = {100, 39, 55, 61, 11, 399};
int* pntrFirst = &array[1]; // here we define pointer to array[1] location
int* pntrSecond = &array[5]; // here we define pointer to array[5] location
// here we subtracting pointers gives the number of elements between them
std::cout << “Array element subtraction value is – ” << pntrSecond – pntrFirst << std::endl; // Result is – 4
return 0;
}
Explanation of subtracting two pointers.
- In this example, the difference between pntrSecond and pntrFirst is 4, because there are 3 elements between array[1] and array[4].
Pointer Arithmetic Operation Concepts with Arrays in C++.
Pointer arithmetic is specifically used when working with array data type elements, because in this case the array name is actually a pointer to the first element. C++ users can use pointer arithmetic concepts to navigate through array data elements.
Example of Pointer Arithmetic Operation.
#include <iostream>
int main() {
int array[] = {89, 39, 55, 61, 11, 44};
int* pntr = array; // Here we define a pointer to the first element of the array.
// Here we use pointer arithmetic to iterate through the entire array.
for (int p = 0; p < 6; p++) {
std::cout << “The element of array ” << p + 1 << ” – ” << *(pntr + p) << std::endl;
}
return 0;
}
Explanation of Pointer Arithmetic Operation.
- In this example, pntr + p moves the pointer forward p elements, and *(pntr + p) accesses and displays the value of the corresponding array element.
Pointer Arithmetic and Multidimensional Array Concepts in C++.
In C++ programming, pointer arithmetic arrays can also be used to access and manage elements in multidimensional arrays. A multidimensional array in C++ is actually an array of arrays (or matrix data blocks), and pointer arithmetic can be applied to individual multidimensional arrays.
Example of Pointer Arithmetic and (2D Array) Multidimensional Array.
#include <iostream>
int main() {
int array[4][4] = {
{9, 1, 6, 5},
{7, 8, 3, 2},
{1, 4, 3, 9}
};
int* pntr = &array[0][0]; // Here we define a pointer to the first element of the 2D multi-dimensional array.
// Here we access elements in the 2D array using pointer arithmetic operations.
for (int p = 0; p < 12; p++) {
std::cout << *(pntr + p) << ” “; // Here we print each 2D multi-dimensional array element.
if ((p + 1) % 4 == 0) std::cout << std::endl; // Here we add a newline after each array row.
}
return 0;
}
Explanation of Pointer Arithmetic and (2D Array) Multidimensional Array.
- In this example, pntr + p accesses and displays the elements of a 2D array (flattened into a single pointer). Even though array is a 2D array, pointer arithmetic displays it as a 1D array, previewing the elements row by row.
Detail summary of pointer arithmetic with array
| Pointer Operation | Pointer Arithmetic Description | Detail Example |
| pntr++ | Increment operator used to move pointer to the next element of the type in index location | pntr++ |
| pntr– | Decrement operator used to move pointer to the previous element of the type | pntr– |
| pnt + n | Here It used to move pointer n positions forward direction (array elements) | pntr + 2 |
| pntr – n | Here It used to move pointer n positions backward direction (array elements) | pntr – 2 |
| pntr1 – pntr2 | Here It used to get the difference between two pointers variable (in elements) | pntr 2 – pntr1 |

