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

Implement Insertion sort Program in C

In this tutorial, we will learn and write a C program to implement insertion sort. Before that you may go through the following topics in …
Read More

C Program for implementation of Selection sort

In this tutorial, we will learn and write a C program to implement selection sort using function. Before that you may go through the following …
Read More

C Program to Delete a Particular element from an Array

In this tutorial, we will write a C program to delete an element in an array by specifying the value. Before that, you should have …
Read More

C Program to sort a String in Ascending order

In this tutorial, we will write a c program to sort a string array in ascending order. Before that, you may go through the following …
Read More

C Program to find Maximum occurring Character in a String

In this tutorial, we will write a program in C to find the maximum occurring character in a string. Before that, you may go through …
Read More

Keith Number Program in C

In this tutorial, we will write a Keith number in C. It is one of the most asked questions in an interview. Before that, you …
Read More