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

1. 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 that 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 data type 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 not affect the value stored at any of the memory locations.

Here is a program that shows both Increment and decrement values of a pointer variable:

#include <stdio.h>

int main()
{
  //declaring the pointer for integer variable
  int a = 5, *x;

  //declaring the pointer for char variable
  char b = 'z', *y;

  //storing the memory location of variable a in pointer variable x
  x = &a;

  /*The corresponding values of the increment and decrement operations on pointer variable x are given below*/
  //printing the actual value of 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
  x--;
  printf("x--= %d\n", x);

  //storing the memory location of variable b in pointer variable y
  y = &b;

  /*The corresponding values of the increment and decrement operations on pointer variable y are given below*/
  //printing the actual value of 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
  y--;
  printf("y--= %d\n", y);

  return 0;
}
Output:-
x= 2012376492
x++= 2012376496
x--= 2012376492
y= 2012376491
y++= 2012376492
y--= 2012376491

2. 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()
{
  //declaring the pointer for integer variable
  int a = 5, *x;

  //declaring the pointer for char variable
  char b = 'z', *y;

  //storing the memory location of variable a in pointer variable x
  x = &a;

  /*The corresponding values of the addition and subtraction operations on pointer variable x are given below*/
  //printing the actual value of x
  printf("x= %d\n", x);

  //the value incremented by 3
  printf("x+3= %d\n", x + 3);

  //the value decremented by 2
  printf("x-2= %d\n", x - 2);

  //storing the memory location of variable b in pointer variable y
  y = &b;

  /*The corresponding values of the addition and subtraction operations on pointer variable y are given below*/
  //printing the actual value of y
  printf("y= %d\n", y);

  //the value incremented by 3
  printf("y+3= %d\n", y + 3);

  //the value decremented by 2
  printf("y-2= %d\n", y - 2);
  return 0;
}
Output:-
x= 593874396
x+3= 593874408
x-2= 593874388
y= 593874395
y+3= 593874398
y-2= 593874393

3. 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;

    //storing the address of num[2] in variable a
    a = &num[2];

    //storing the address of num[6] in variable b
    b = &num[6];

    printf("a = %d\n", a);
    printf("b = %d\n", b);

    //prints the number of elements between the two elements indicated by the pointers
    printf("a-b = %d\n", b - a);

    //prints the difference in value of the two elements
    printf("*a-*b = %d\n", *a - *b);

    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.


4. 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;

    //storing the address of num[2] in variable a
    a = &num[2];

    //base address plus 2 stores the address of num[2] in the variable b
    b = (num + 2);

    //storing the address of num[6] in variable b
    c = &num[6];

    //Print values of all the pointers
    printf("a= %d\n", a);
    printf("b= %d\n", b);
    printf("c= %d\n", c);

    //comparing for equality
    if (a == b)
        printf("a and b point to the same location and the value is: %d\n", *a);

    //comparing for inequality
    if (a != c)
        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

5. 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

Helpful Links

Please follow C Programming tutorials or the menu in the sidebar for the complete tutorial series.

Also for the example C programs please refer to C Programming Examples.

All examples are hosted on Github.


Recommended Books


An investment in knowledge always pays the best interest. I hope you like the tutorial. Do come back for more because learning paves way for a better understanding

Do not forget to share and Subscribe.

Happy coding!! ?

Recommended -

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x
Index