Pointer Arithmetic in C++ (++, –, +, -, ==)

programming

Summary: C++ allows a few arithmetic operations such as increment, decrement, adding and subtracting a number from a pointer, etc to be used with pointers. In this tutorial, we will discuss all such pointer arithmetic with examples.

Recommended: What are Pointers?

Introduction to Pointer Arithmetic

Using arithmetic operations on a number changes its value, likewise using arithmetic operation on pointer changes the address value.

The change in the address value depends on the size of the pointer type and the type of arithmetic operation.

Incrementing (++) or Decrementing (–) Pointers

Incrementing a pointer increases the stored address by the size of its type.

For example, incrementing an integer pointer will increase the stored address by 4 because the size of the int type in C++ is 4 bytes.

Incrementing the integer pointer.

Similarly, decrementing a pointer decreases the value of stored address by the size of its type.

Incrementing or decrementing a pointer can be used to traverse an array because the difference between the address of the two consecutive array elements is equal to the size of its type (e.g. 4 bytes in case of an integer array).

Array elements memory address
#include <iostream>
using namespace std;
 
int main()
{
    int array[] = {4, 5, 6, -1};
    int *ptr = array; 
    
    while(*ptr != -1){
        //derefrencing to access value
        cout << *ptr << " ";
        //Incrementing to point to the next element
        ptr++;                  
    }
    return 0;
}

Output:

4 5 6

Adding (+) or Subtracting (-) Numbers on Pointers

Adding n to a pointer, increments it n times i.e. adds n*sizeof(type) to the address stored in the pointer.

Example– Adding 3 to the int type pointer:

Addition on pointers

Similarly, subtracting n from a pointer decreases the stored address by n*sizeof(type).

One of the applications of adding numbers to a pointer is accessing the elements of an array:

#include <iostream>
using namespace std;
 
int main()
{
    int array[]={4, 5, 6, -1};
    int *ptr = array; 
 
    cout << *ptr << " ";
    cout << *(ptr+1) << " ";    
    cout << *(ptr+2) << " ";
 
    return 0;
}

Output:

4 5 6

The pointer is initially pointing to the first array element. On adding 1, it gets incremented by 4 and points to 2nd element of the array and so on.

Subtracting two Pointers

Subtracting two pointers of the same type gives the number of elements between them.

#include <iostream>
using namespace std;
 
int main()
{
    int array[]={4, 5, 6, -1};
    int *ptr = array; 
    
    //Loop until pointer points to last element 
    while(*ptr != -1){
        ptr++;         
    }
 
    //elements between 4(inclusive) and -1 
    cout << ptr - array;
 
    return 0;
}

Output:

3

Comparing two pointers

We can compare two pointers using the == operator to check whether they point to the same location or not.

Pointers pointing to same and different locations

If two pointers point to the same location then definitely the dereferenced value will be the same, but if they point to different locations doesn’t mean the values will be different.

Here is an example illustrating the same:

#include <iostream>
using namespace std;
 
int main()
{
    //String values
    string str1 = "Pencil";
    string str2 = "Pencil";
    
    //Pointers
    string* ptr1 = &str1;    //points to str1
    string* ptr2 = &str2;    //points to str2
 
    //Comparing address
    cout << "ptr1 == ptr2 : " << (ptr1 == ptr2) << endl;
    //Comparing values
    cout << "*ptr1 == *ptr2 : " << (*ptr1 == *ptr2) << endl;

    return 0;
}

Output:

ptr1 == ptr2 : 0
*ptr1 == *ptr2 : 1


Simply using the == operator on pointer does not compare the data which they point to. To compare the data referenced by two pointers, dereference it using the * operator.

In this tutorial, we learned with examples the different types of pointer arithmetic in C++ such as adding or subtracting numbers to the pointers, incrementing or decrementing pointer, etc.

4 thoughts on “Pointer Arithmetic in C++ (++, –, +, -, ==)”

  1. Hey, thanks a lot for showing subtracting two pointers program, it helped me with my assignment. Is it possible to show addition of 2 pointers as well?

    1. Good question Mukta.

      C++ does not support subtraction of pointers, because it is assumed that it has no significance.

      Think in this way, subtracting the house’s number (address) of any two houses in a certain colony would give us the number of houses between them (e.g. 126 – 123 = 3), but if we add them (126 + 123 = 249) we do not get any meaningful information.

      Hope it helps.

Leave a Reply

Your email address will not be published. Required fields are marked *