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

Java Program to find the sum of the Largest Forward Diagonal

in this tutorial, we will write a java program to find the sum of the Largest Forward Diagonal in an Arraylist (matrix). Java Program to …

C Program to search an element in an array using Pointers

A separate function( search_function()) will be created where the array pointer will be declared and the searched element along with the size of an array …

C Program to find the sum of the digits of a number using recursion function

This C program calculates the sum of digits of a given number using recursion. Here’s a concise explanation: Function Definition: sumDigits(int n) This function calculates …

C program to find factorial of a number using Ternary operator with Recursion

Recursion refers to the function calling itself directly or in a cycle. Before we begin, you should have the knowledge of following in C Programming: …

C Program to Add Two Numbers Using Call by Reference

The program takes the two numbers from the user and passes the reference to the function where the sum is calculated. You may go through …

Find the output ab, cd, ef, g for the input a,b,c,d,e,f,g in Javascript and Python

In this tutorial, we will write a program to find a pairs of elements from an array such that for the input [a,b,c,d,e,f,g] we will …