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


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:


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.



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:


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.

Leave a Reply