C – Pointer Arithmetic4 min read

In this tutorial, we will learn about the various operation that can be performed on Pointers. But before that if you do not know about the Pointer click the link below.


As the operation can be performed on numeric value and pointer is an address which is an numeric value so we can perform some operation on Pointers. There are specifically four arithmetic operators that can be performed on the Pointers and those are:

  • Increment ++
  • Decrement - -
  • Addition +
  • Substraction -
  • Comparision ==

Let us assume that a pointer (ptr) is pointing to some integer variable whose address is 2000. Now let us perform increment operation on that pointer ptr.

ptr++

Now where the ptr points to? Well the ptr address value which was initially 2000 increases to 2004 because of the integer. The integer occupies 4 bytes of storage and so ptr increases by 4. The increment depends on the data-types that you are pointing through your pointer. If your pointer points to character type then the address which was initially at 2000 will increases to 2001 (as the size of the character is 1 byte).


Increment of Pointer in C

As from the above explanation, we conclude that the increment of the pointer by 1 means the pointer value will increase by the size of the data-types that the pointer is pointing.

It is mostly used during the traversing of an array through which the program can reach every element present in an array.

Example for incrementing pointer variable in C.

Output: Check the last two digit which is increased by 4 byte (int).

Example of Traversing an array by incrementing pointer variable in C.

Output: Following result is produced after compilation and execution.


Decrement of Pointer in C

Decrement is as same as the increment. We can decrement the value of the pointer depending on its data types the pointer is pointing.

Example for decrementing pointer variable in C.

Output: Check the last two digit which is decreased by 4 byte (int).


Addition and Subtraction of Pointers in C

We can add or subtract the value of the pointer variable. If you add 2 that means the value of the pointer increases 2 times depending on the size of data types that the pointer pointing to such as,

present_address + (number  ×  size_of_the_data_type)

Subtraction is also processed in the same manner instead of increases 2 times, pointer will be decreased by 2 times the size of the data type.

Example for addition and subtraction of pointer variable in C.

Output: Check the last two digit change.


Pointers Comparision

Pointers can be compared using  ==, <, and > operators in C if they are pointing to the same array.

Example of comparing the pointers in C

Output: *ptr points to the actual value stored in the address that the pointer points to.


Operation that can’t be operated on Pointer:

The following operators are not allowed on the pointer address value as it gives the false address value. You can say that they are illegal operations on Pointers.

  1. pointer_address + pointer_address
  2. pointer_address * pointer_address
  3. pointer_address % pointer_address
  4. pointer_address / pointer_address
  5. pointer_address & pointer_address
  6. pointer_address ^ pointer_address
  7. pointer_address | pointer_address
  8. ~pointer_address

MORE

C++ Structures

In the C/C++ programming language, the structure is user-defined data types that allow us to store the combination of different data types together. All the …
Read More

C++ Static Keyword

Static s a keyword in C++ that is when used with different types gives different meanings. static keyword can be used with variables in a …
Read More

C++ Templates

Templates in C++ are the powerful feature that allows us to write generic programs (generic classes and generic functions). A single class or function is …
Read More

C++ Friend Function

There are private and protected members in a class that is inaccessible from outside of the class, constituting one of the concepts of object-oriented programming …
Read More

this Pointer in C++

In C++, this is a keyword that refers to the current instance of a class and this pointer holds the address or points to the …
Read More

Inheritance in C++

Inheritance is one of the most important concepts of object-oriented programming. In C++, inheritance is the process of deriving the properties and behaviors of one …
Read More