In this article pointer arithmetic in c we give the information about pointer is a variable, it has an address. We can manipulate this address by arithmetic operations and when we do this, it is called Pointer Arithmetic.

Pointer Arithmetic in C:

As we know, pointer is a variable which contains the address of another variable and since pointer is also a variable, it also has an address. We can manipulate this address by arithmetic operations (addition, subtraction etc.) and when we do this, it is called Pointer Arithmetic.

Pointer arithmetic is slightly different from normal arithmetic.

C language provides us some set of operators for Pointer Arithmetic operations which are as follows -:

Increment

Decrement

Addition

Subtraction

Comparison

1) Increment

When we use increment operator (++) with pointer variable then it returns the next address. This next address is equal to the sum of the current pointer address and the size of the pointer (4 bytes according to 32 bit system).

For example -: If we have an integer pointer ip whose address is 1000, then increasing it by 1 will give us 1004 (i.e. 1000 + 1 * 4) instead of 1001 because the size of int data type is 4 bytes. But if we are using a system where the size of int is 2 bytes then we will get 1002 (i.e. 1000 + 1 * 2) which will be the next memory address.

Let us understand these things with an example.

Example -:

int i = 12, *ip = &i;

double d = 2.3, *dp = &d;

char ch = ‘a’, *cp = &ch;

Suppose the address of i, d and ch is 1000, 2000, 3000 respectively.

Pointer Arithmetic on Integers 

Pointer Expression How it is evaluated?
ip = ip + 1 ip => ip + 1 => 1000 + 1*4 => 1004
ip++ or ++ip ip++ => ip + 1 => 1004 + 1*4 => 1008

Pointer Arithmetic on Float

Pointer Expression How it is evaluated ?
dp + 1 dp = dp + 1 => 2000 + 1*8 => 2008
dp++ or ++dp dp++ => dp+1 => 2008+1*8 => 2016

Pointer Arithmetic on Characters 

Pointer Expression How it is evaluated ?
cp + 1 cp = cp + 1 => 3000 + 1*1 => 3001
cp++ or ++cp cp => cp + 1 => 3001 + 1*1 => 3002

Note -:

Performing arithmetic operation on a pointer of char type looks like a normal arithmetic operation because the size of char data type is 1 byte.

Generally, we use pointer arithmetic with arrays because array elements are arranged in contiguous memory.

// Program 

#include<stdio.h>

void main()

{

    int i = 23, *ip = &i;

    double d = 3.2, *dp = &d;

    char ch = ‘a’, *cp = &ch;

    printf(“Value of ip = %u\n”, ip);

    printf(“Value of dp = %u\n”, dp);

    printf(“Value of cp = %u\n\n”, cp);

    printf(“Value of ip + 1 = %u\n”, ip + 1);

    printf(“Value of dp + 1 = %u\n”, dp + 1);

    printf(“Value of cp + 1 = %u\n\n”, cp + 1);

}

Output -:

Value of ip = 53316

Value of dp = 53304

Value of cp = 53303

Value of ip + 1 = 53320

Value of dp + 1 = 53312

Value of cp + 1 = 53304

2) Decrement (–)

When we use decrement operator (- -) with pointer variable then it returns the previous address. This previous address is equal to the subtraction of the current pointer address and the size of the pointer (4 bytes in 32-bit systems).

For example -: If we have an integer pointer ip whose address is 1000, then by subtracting it by 1 we will get 996 (i.e. 1000 – 1 * 4) instead of 999 because the size of int data type is 4 bytes. But if we are using a system where the size of int is 2 bytes then we will get 998 (ie 1000 – 1 * 2).

Let us understand these things also with an example.

Example -:

int i = 12, *ip = &i;

double d = 2.3, *dp = &d;

char ch = ‘a’, *cp = &ch;

Suppose the address of i, d and ch is 1000, 2000, 3000 respectively.

Pointer Expression How it is evaluated?
ip– or –ip ip => ip – 1 => 1000 – 1*4 => 996
dp– or –dp dp => dp – 1=>2000 – 1*8=>1992`
cp– or –cp cp => cp – 1 => 3000 – 1*1 => 2999

Pointer Arithmetic in C:

 3) Addition

When we add a value to a pointer, it is first multiplied by a value equal to the pointer size and then added to the pointer; from this we get the next pointer address.

Its syntax is something like this -:

Syntax 

new_address = current_address + (number * size_of(data type))

Example program

#include<stdio.h>

int main()

{

int num = 15;

int *p;

//stores the address of num variable   p = #

printf(“Address of p variable is %u \n”,p);

//adding 2 to pointer variable

p=p+2;

printf(“After adding 2: Address of p variable is %u \n”,p);

return 0;

}

Output -:

Address of p variable is 34302

After adding 2: Address of p variable is 34310

4) Subtraction

Just as we perform addition operations with pointers, we can also perform subtraction operations with pointers. When we decrement a value in a pointer, it returns an address which is just the previous address of that pointer. Its syntax is something like this -:

Syntax 

new_address= current_address – (number * size_of(data type))

//Program

#include<stdio.h>

int main()

{

int num = 15;

int *p;

//stores the address of number variable    p=#

printf(“Address of p variable is %u \n”,p);

//Subtracting 2 to pointer variable

p=p-2;

printf(“After Subtracting 2: Address of p variable is %u \n”,p);

return 0;

}

Output -:

Address of p variable is 34310

After Subtracting 2: Address of p variable is 34302

5) Comparison

We can use relational operators ( <, <=, >, >= , == , !=) with pointers.

== and != operators are used to compare two pointers.

Using other relational operators (<, <=, >, >=) to compare two pointers is meaningful only when they both point to the same array elements.

Pointer comparisons (<, <=, >, >= , == , !=) are not used much compared to pointer arithmetic (addition, subtraction).

Comparisons are useful if you want to check whether two pointers are pointing to the same location.

// Program 

#include <stdio.h>

int main()

{

    int num = 10;

    int *ptr1 = #    // ptr1 points to num

    int *ptr2 = #    // ptr2 also points to num

    if(ptr1 == ptr2)

    {

        // Do some task

        printf(“Both pointers points to same memory location”);

    }

    return 0;

}

Output:-

Both pointers points to same memory location

Subtraction of Two Pointers:-

Subtraction between two pointers is possible only if both of them are of the same data type.

Program:-

// C program to illustrate Subtraction of two pointers

#include <stdio.h>

int main()

{

            int x;

            // Integer variable

            int N = 4;

            // Pointer to an integer

            int *ptr1, *ptr2;

            // Pointer stores the address of N

            ptr1 = &N;

            ptr2 = &N;

            // Incrementing ptr2 by 3

            ptr2 = ptr2 + 3;

            // Subtraction of ptr2 and ptr1

            x = ptr2 – ptr1;

            // Print x to get the Increment between ptr1 and ptr2

            printf(“Subtraction of ptr1 & ptr2 is %d\n”, x);

            return 0;

}

Output:- 

Subtraction of ptr1 & ptr2 is 3

Rules for Performing Pointer Arithmetic:-

There are many operations that cannot be performed on pointers. Since a pointer stores an address, we should ignore operations that may provide an illegal address.

We cannot perform addition, multiplication and divisional operations between two addresses, but we can perform subtraction.

We can subtract addresses of the same type.

We cannot multiply and divide any integer value with address, but we can definitely add and subtract integer value with address.

The value obtained by adding or subtracting an integer value from an address is also an address.

Some More: 

POP- Introduction to Programming Using ‘C’

DS – Data structure Using C

OOP – Object Oriented Programming 

Java Programming

DBMS – Database Management System

RDBMS – Relational Database Management System

Join Now: Data Warehousing and Data Mining 

Leave a Reply

Your email address will not be published. Required fields are marked *