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

String Pattern Programs in C

In this tutorial, we will write various C pattern programs for String. Before that, you may go through the following topics in C. for loop …
Read More

Java Program to Find pair of Integers in Array whose sum is given Number

In this tutorial, we will write a program to find a pair of elements from an array whose sum equals a given number in java …
Read More

Program to Print Diamond Alphabet Patterns in C

In this tutorial, we will learn to write a C program to print Diamond patterns using alphabets/characters. However, in this tutorial, we will create a …
Read More

Half Diamond Pattern in C using Alphabets

In this tutorial, we will learn and code the half diamond alphabet patterns in C programming language. However, in this tutorial, we will create a …
Read More

Half Pyramid of Alphabets in C

In this tutorial, we will learn and code alphabet patterns in C programming language specifically the Half pyramid of alphabets in C programming. However, in …
Read More

Inverted Half Pyramid Pattern of Alphabets in C

In this tutorial, we will write a C program to print half Pyramid using alphabets/characters. Before that, you may go through the following topic in …
Read More