Operations on Pointers-C Programming Language

Pointers are variables that contain the memory address of another variable. Since an address in a memory is a numeric value we can perform arithmetic operations on the pointer values. The different operations that can be possibly performed on pointers are:

  • Incrementing/Decrementing a pointer
  • Addition/Subtraction of a constant number to a pointer
  • Subtraction of one pointer from another
  • Comparison of two pointers

Incrementing/Decrementing a Pointer

Any pointer variable when incremented points to the next memory location of its type. For example:

int a=5,*x;
x=&a;
x++;

Now, let us assume that the memory location where a is stored is 63420. So, x now contains the value 63420.Also, note that x stores the address of an integer variable which has a size of 4 bytes. Once the value of x is incremented it does not store 63421, rather it stores 63434 which is the next memory location for the integer variable. Thus, the pointer gets incremented according to the datatype of the value it stores. The same is the case for decrementing the value of a pointer variable. It points to the previous memory location of its type. These operations do no affect the value stored at any of the memory locations. Here is a program which shows both Increment and decrement values of a pointer variable:

#include <stdio.h>
int main()
{
    int a=5,*x;//declaring the pointer for integer variable
    char b='z',*y;//declaring the pointer for char variable
    x=&a;//storing the memory location of variable a in pointer variable x
    /*The corresponding values of the increment and decrement operations on pointer variable x are given below*/
    printf("x= %d\n",x);//printing the actual value of x
    x++;
    printf("x++= %d\n",x);//the value gets incremented by 4 bytes because the size of one int variable is 4 bytes
    x--;
    printf("x--= %d\n",x);//the value gets decremented by 4 bytes and changes back to the original value
    y=&b;//storing the memory location of variable b in pointer variable y
    /*The corresponding values of the increment and decrement operations on pointer variable y are given below*/
    printf("y= %d\n",y);//printing the actual value of y
    y++;
    printf("y++= %d\n",y);//the value gets incremented by 1 byte because the size of one char variable is 1 bytes
    y--;
    printf("y--= %d\n",y);//the value gets decremented by 1 byte and changes back to the original value
    return 0;
}

Output:-
x= 2012376492
x++= 2012376496
x--= 2012376492
y= 2012376491
y++= 2012376492
y--= 2012376491

Addition/Subtraction of a constant number to a pointer

Addition or subtraction of a constant number to a pointer is allowed. The result is similar to the increment or decrement operator with the only difference being the increase or decrease in the memory location by the constant number given. Also, not to forget the values get incremented or decremented according to the type of variable it stores. The following program shows an example of addition and subtraction of a constant number to a pointer:
Note: – Output may vary every time the program is run because memory locations may differ with each execution.

#include <stdio.h>
int main()
{
    int a=5,*x;//declaring the pointer for integer variable
    char b='z',*y;//declaring the pointer for char variable
    x=&a;//storing the memory location of variable a in pointer variable x
    /*The corresponding values of the addition and subtraction operations on pointer variable x are given below*/
    printf("x= %d\n",x);//printing the actual value of x
    printf("x+3= %d\n",x+3);//the value incremented by 3
    printf("x-2= %d\n",x-2);//the value decremented by 2
    y=&b;//storing the memory location of variable b in pointer variable y
    /*The corresponding values of the addition and subtraction operations on pointer variable y are given below*/
    printf("y= %d\n",y);//printing the actual value of y
    printf("y+3= %d\n",y+3);//the value incremented by 3
    printf("y-2= %d\n",y-2);//the value decremented by 2
    return 0;
}

Output:-
x= 593874396
x+3= 593874408
x-2= 593874388
y= 593874395
y+3= 593874398
y-2= 593874393

Subtraction of one pointer from another

A pointer variable can be subtracted from another pointer variable only if they point to the elements of the same array. Also, subtraction of one pointer from another pointer that points to the elements of the same array gives the number of elements between the array elements that are indicated by the pointer. The following example shows this:

#include <stdio.h>
int main()
{
    int num[10]={1,5,9,4,8,3,0,2,6,7},*a,*b;
    a=&num[2];//storing the address of num[2] in variable a
    b=&num[6];//storing the address of num[6] in variable b
    
    printf("a = %d\n",a);
    printf("b = %d\n",b);
    printf("a-b = %d\n",b-a);//prints the number of elements between the two elements indicated by the pointers
    printf("*a-*b = %d\n",*a-*b);//prints the difference in value of the two elements
    
    return 0;
}

Output:-
a= 2686680
b= 2686696
a-b = 4
*a-*b = 9

We get the result as 4 which is not the arithmetic difference of the address values of the two variables. We rather get the number of elements separating the corresponding array elements. *a-*b gives the difference of the values stored in the respective positions in the array.


Comparison of two pointers

Comparison of two pointer variables is possible only if the two pointer variables are of the same type. It becomes more convenient if they point to the elements of the same array. These comparisons are to check equality or inequality. The result is true if both the pointers point to the same location in the memory and false if they point to different locations in the memory. The program below shows the output of pointer comparisons:

#include <stdio.h>
int main()
{
    int num[10]={1,5,9,4,8,3,0,2,6,7},*a,*b,*c;
    a=&num[2];//storing the address of num[2] in variable a
    b=(num+2);//base address plus 2 stores the address of num[2] in the variable b
    c=&num[6];//storing the address of num[6] in variable b
    /*Print values of all the pointers*/
    printf("a= %d\n",a);
    printf("b= %d\n",b);
    printf("c= %d\n",c);
    if(a==b)//comparing for equality
        printf("a and b point to the same location and the value is: %d\n",*a);
    if(a!=c)//comparing for inequality
        printf("a and c do not point to the same location in the memory");
    return 0;
}

Output:-
a= 2686676
b= 2686676
c= 2686692
a and b point to the same location and the value is: 9
a and c do not point to the same location in the memory

Operations not possible with pointers

There are a few operations that are not possible with pointers. These are:

  • Addition of two pointer variables
  • Multiplication of a pointer with a constant value
  • Division of a pointer with a constant value

Learning never exhausts the mind.So, do come back for more. Hope this helps and you like the tutorial. Do ask for any queries in the comment box and provide your valuable feedback. Share and subscribe.

Keep Coding!! Happy Coding!! :)