Arithmetic Operations on Pointers in C

Arithmetic Operations on Pointers in C Language with Examples

In this article, I am going to discuss Arithmetic Operations on Pointers in C Language with Examples. Please read our previous articles, where we discussed Pointers in C Language with Examples. At the end of this article, you will understand what are the different arithmetic operations we can perform on the pointer in C language.

Arithmetic Operations on Pointers in C:

As we already discussed in our previous article, the Pointers variables are used to store the address of another variable. The address is nothing but the memory location assigned to the variable. That means the pointer doesn’t store any value, it stores the address. Hence, there are only a few operations that are allowed to perform on Pointers. The operations are slightly different from the ones that we generally use for mathematical calculations. The operations are as follows:

  1. Increment/Decrement of a Pointer
  2. Addition of integer to a pointer
  3. Subtraction of integer to a pointer
  4. Subtracting two pointers of the same type
Increment/Decrement Operation of a Pointer in C Language

Increment: When a pointer is incremented, it actually increments by the number equal to the size of the data type for which it is a pointer. For Example, If the pointer is of type integer and if the pointer stores address 1000 and if the pointer is incremented, then it will increment by 2(size of an int), and the new address that the pointer will point to 1002. While if the pointer is of type float and if it stores the address 1000 and if the pointer is incremented then it will increment by 4(size of a float) and the new address will be 1004.

Decrement: It is just the opposite of the Increment operation. When a pointer is decremented, it actually decrements by the number equal to the size of the data type for which it is a pointer. For Example, If the pointer is of type integer and if the pointer stores the address 1000 and if the pointer is decremented, then it will decrement by 2(size of an int), and the new address that the pointer will point to is 998. While if the pointer is of type float and if it stores the address 1000 and if the pointer is decremented then it will decrement by 4(size of a float) and the new address will be 996.

Program to Understand Increment/Decrement Operation of a Pointer

The following program illustrates pointer increment/decrement operation using C Language.

#include <stdio.h>
int main ()
{
    // Integer variable
    int a = 10, b=20;

    // Pointer to an integer
    int *ptr1, *ptr2;

    // Pointer stores the address of variable a
    ptr1 = &a;
    ptr2 = &b;

    printf ("Pointer ptr1 before Increment: ");
    printf ("%p \n", ptr1);

    // Incrementing pointer ptr1;
    ptr1++;

    printf ("Pointer ptr1 after Increment: ");
    printf ("%p \n\n", ptr1);

    printf ("Pointer ptr2 before Decrement: ");
    printf ("%p \n", ptr2);

    // Decrementing pointer ptr2;
    ptr2--;

    printf ("Pointer ptr2 after Decrement: ");
    printf ("%p \n\n", ptr2);

    return 0;
}
Output:

Increment and Decrement Operation of a Pointer in C Language

Addition and Subtraction Arithmetic Operation on Pointer in C Language

Let us see how to perform Addition and Subtraction Operation on Pointer in C Language with an example.

  1. Addition: In C Programming Language, When a pointer is added with a value, then the value is first multiplied by the size of the data type and then added to the pointer.
  2. Subtraction: In C Programming Language, When a pointer is subtracted with a value, then the value is first multiplied by the size of the data type and then subtracted from the pointer.

The following program illustrates Addition and Subtraction Arithmetic Operation on Pointer in C Language.

#include <stdio.h>
int main()
{
    // Integer variable
    int a = 10;
 
    // Pointer to an integer
    int *ptr1, *ptr2;
 
    // Pointer stores the address of variable a
    ptr1 = &a;
    ptr2 = &a;
 
    printf("Pointer ptr1 before Addition: ");
    printf("%p \n", ptr1);
 
    // Addition of 2 to pointer ptr1
    ptr1 = ptr1 + 2;
    printf("Pointer ptr1 after Addition: ");
    printf("%p \n", ptr1);

    printf("Pointer ptr2 before Subtraction : ");
    printf("%p \n", ptr2);
 
    // Subtraction of 2 from pointer ptr2
    ptr2 = ptr2 + 2;
    printf("Pointer ptr1 after Subtraction : ");
    printf("%p \n", ptr2);
    return 0;
}
Output:

Addition and Subtraction Arithmetic Operation on Pointer in C Language

Addition and Subtraction of two Pointers in C Language

In the below example, we have created two integer pointers and then perform the addition and subtraction operation.

#include<stdio.h>
int main ()
{
    int a = 30, b = 10, *p1, *p2, sum;
    p1 = &a;
    p2 = &b;
    sum = *p1 + *p2;
    printf ("Addition of two numbers = %d\n", sum);
    sum = *p1 - *p2;
    printf ("Subtraction of two numbers = %d\n", sum);
    return 0;
}
Output:

Arithmetic Operations on Pointers in C

Note: The addition and subtraction of two pointers is possible only when they have the same data type.

 

Pointer Operations in C

Pointer Operations in C

Pointers Rule:
Rule 1:
  • Address + Number // Address (Next Address)
  • Address – Number // Address (Pre-Address)
  • Address++ // Address (Next Address)
  • Address– // Address (Pre-Address)
  • ++Address // Address (Next Address)
  • –Address // Address (Pre-Address)
Rule 2:
  • Address-Address //Number (No. of elements) //size difference / size of (datatype)

Example:
int*p1=(int*)100
int*p2=(int*)200
p2-p1=50
200-100=100 / size of (int)

Rule 3:
  • Address + Address = illegal
  • Address * Address = illegal
  • Address / Address = illegal
  • Address % Address = illegal
Rule 4:

We can’t perform a bitwise operation between 2 pointers like

  • Address & Address = illegal
  • Address | Address = illegal
  • Address ^ Address = illegal
  • ~Address = illegal
Rule 5:

We can use relational operator and conditional operator (<,>, <=,>=, ==, !=, ?:) between 2 pointers

  • Address > Address = T/F
  • Address >= Address = T/F
Rule 6:

We can find the size of a pointer using the sizeof operator.

Accessing value through Pointer in C Language:

This can be done by using the indirection operator, so this operator is also known as the value at the address operator.

Program to Accessing value through Pointer in C Language:
#include<stdio.h>
void main()
{
   int a1;
   int*ptr;
   printf("Enter a value\n");
   scanf("%d", &a1);
   ptr=&a1;
   printf("\n%d", *ptr);
}
Output:

Program to Accessing value through Pointer in C Language

Pointer Conversions:
  • Suspicious pointer conversion: The warning message occurs when we are assigning the address of a variable in a different type of pointer. These conversions are not allowed in C++.
  • Non-portable pointer conversion: This warning message occurs when we are assigning value type data to a pointer.
Example:
void main()
{
   int a;
   int*ptr;
   ptr=a;
}

In the next article, I am going to discuss Pointer-to-Pointer in C language. Here, in this article, I try to explain Arithmetic Operations on Pointers in C Language with Examples. I hope you enjoy this Arithmetic Operations on Pointers in C Language with Examples article. I would like to have your feedback. Please post your feedback, question, or comments about this article.

Leave a Reply

Your email address will not be published.