Pointer Arithmetic in C/C++
Pointer arithmetic is an important concept in C and C++ programming. It involves performing arithmetic operations such as addition and subtraction on pointers, allowing you to navigate through arrays and other data structures efficiently. Let’s dive into some common operations and how they work.
Common Operations
Incrementing (ptr++) and Decrementing (ptr — ) Pointers:
- Incrementing: Moves the pointer to the next memory location.
- Decrementing: Moves the pointer to the previous memory location.
Adding an Integer to a Pointer (ptr + n):
- This operation moves the pointer forward by
nelements of its type. - Example: If
ptrpoints to the 1st element of an array,ptr + 2points to the 3rd element.
Subtracting an Integer from a Pointer (ptr — n):
- This operation moves the pointer backward by
nelements of its type. - Example: If
ptrpoints to the 3rd element of an array,ptr - 2points to the 1st element.
Comparison of Pointers (<, >, >=, <=, ==, !=):
- Pointers can be compared to determine their relative positions in memory.
- Example:
if (ptr1 < ptr2)checks ifptr1points to a memory location beforeptr2.
Increment and Decrement Example
Imagine a memory block represented as follows:
Memory: [996, 997, 998, 999, 1000, 1001, 1002, 1003, 1004]Get Codewithcoders’s stories in your inbox
Join Medium for free to get updates from this writer.
- Incrementing (ptr++):
- If
ptris at address 1000,ptr++will move it to 1004 (assuming 32-bit integers, where each int is 4 bytes). - Decrementing (ptr — ):
- If
ptris at address 1000,ptr--will move it to 996.
Adding an Integer to a Pointer
Consider the memory layout:
Memory: [1000, 1004, 1008, 1012, 1016, 1020, 1024, 1028, 1032]If ptr points to address 1000, ptr + 3 will move it to address 1012 (3 * 4 bytes for 32-bit integers).
Subtracting an Integer from a Pointer
Using the same memory layout:
Memory: [1000, 1004, 1008, 1012, 1016, 1020, 1024, 1028, 1032]If ptr points to address 1020, ptr - 5 will move it to address 1000 (5 * 4 bytes backward for 32-bit integers).
Example Code
Here’s a simple example demonstrating the use of pointers in a C++ program:
#include <iostream>
using namespace std;
int main(){
// Declare and init an array
int arr[5] = {10, 20 , 30 , 40 , 50 };
// pointer point first element of array
int *ptr = arr;
// print address and value of pointer
std::cout << "address of pointer is "<<ptr<<", and value of pointer is: "<<*ptr << std::endl;
// // incremnet the pointer
// ptr++;
// // incremented value of pointer
// std::cout << "incremented address of pointer "<<ptr <<", and incremented value of pointer is: "<<*ptr << std::endl;
// // Decrement the pointer
// ptr--;
// // print address and value of decremented pointer
// std::cout << "Decremented address of pointer "<<ptr <<", and Decremented value of pointer is: "<<*ptr << std::endl;
// adding 2 to move forward
// ptr = ptr + 2 ;
// // print
// std::cout << "after adding 2 "<<ptr <<", the value of pointer "<<*ptr << std::endl;
// // adding 2 to move backward
// ptr = ptr - 2 ;
// // print
// std::cout << "after adding -2 "<<ptr <<", the value of pointer "<<*ptr<<std::endl;
// comparsion operator
int *ptr1 = &arr[1];
int *ptr2 = &arr[2];
if(ptr1 < ptr2){
std::cout << "ptr1 come first in memory address then ptr2" << std::endl;
}
else if (ptr1 > ptr2 )
{
std::cout << "ptr2 is come first in memory address then ptr2" << std::endl;
}
else{
std::cout << "ptr1 and ptr2 both are same memory location" << std::endl;
}
if (ptr1 == &arr[1])
{
std::cout << "ptr1 points to the 2nd element of the array" << std::endl;
}
return 0;
}Key Points to Remember
- Pointer arithmetic depends on the data type: The increment or decrement steps are determined by the size of the data type the pointer points to.
- Pointer comparison: Useful for navigating through arrays or determining the relative positions of elements.
Watch My YouTube Video
For a more in-depth explanation, watch my YouTube video on this topic:







