Aug 10, 2014

C Programming #42: Pointer - arithmetic

This article discusses the arithmetic operators that can be used with pointer variables. Since we already know that pointers contain addresses, not all operation makes sense from address point of view. Some of operators behave differently when it comes to pointers. Some won't work at all.


Pointer Increment 

Let me demonstrate how it works by giving a example program.


#include <stdio.h>
int main()
{
   int i, *pi = &i;
   int f, *pf = &f;
   int c, *pc = &c;

   printf("Pointer pi is %p\n", pi);
   printf("Pointer pf is %p\n", pf);
   printf("Pointer pc is %p\n", pc);

   pi++;
   pf++;
   pc++;

   printf("After increment, Pointer pi is %p\n", pi);
   printf("After increment, Pointer pf is %p\n", pf);
   printf("After increment, Pointer pc is %p\n", pc);

   return 0;
}

Output of program is as follows


Pointer pc is 0x7fff0d16d9df
Pointer pi is 0x7fff0d16d9d4
Pointer pf is 0x7fff0d16d9d8
After increment, Pointer pc is 0x7fff0d16d9e0
After increment, Pointer pi is 0x7fff0d16d9d8
After increment, Pointer pf is 0x7fff0d16d9dc

We knew till now that increment increments value by 1. But when it comes to incrementing pointers.
  • increments increases by 1 when it is character pointer.
  • increments increases by 4 when it is integer pointer.
  • increments increases by 4 when it is float pointer.
In general it increments by sizeof the datatype to which pointer is pointing to. Pre-increment and post-increment of pointers works similar to how it would work other variables.


Pointer Decrement

We knew till now that decrement decreases value by 1. But when it comes to decrement pointers.
  • decrement decreases by 1 when it is character pointer.
  • decrement decreases by 4 when it is integer pointer.
  • decrement decreases by 4 when it is float pointer.
In general it decrements by sizeof the datatype to which pointer is pointing to. Pre-decrement and post-decrement of pointers works similar to how it would work other variables.


Pointer integer addition

We cannot add two pointers, it would not make any sense. But adding a integer to pointer makes sense and is demonstrated in following program.


#include <stdio.h>
int main()
{
   char c, *pc = &c;
   int i, *pi = &i;
   float f, *pf = &f;

   printf("Pointer pc is %p\n", pc);
   printf("Pointer pi is %p\n", pi);
   printf("Pointer pf is %p\n", pf);

   pi = pi + 2;
   pf = pf + 2;
   pc = pc + 2;                                                                 

   printf("After increment, Pointer pc is %p\n", pc);
   printf("After increment, Pointer pi is %p\n", pi);
   printf("After increment, Pointer pf is %p\n", pf);


   return 0;
}

Output of above program is as follows.


Pointer pc is 0x7fffdf267c0f
Pointer pi is 0x7fffdf267c04
Pointer pf is 0x7fffdf267c08
After increment, Pointer pc is 0x7fffdf267c11
After increment, Pointer pi is 0x7fffdf267c0c
After increment, Pointer pf is 0x7fffdf267c10
Explanation
  • Adding 2 to character pointer increases by 2 ( 2 * sizeof(char))
  • Adding 2 to integer pointer increases by 8 ( 2 * sizeof(int))
  • Adding 2 to float pointer increases by 8 (2 * sizeof(floar))
Hence we can conclude that adding n to pointer increases pointer value by n * sizeof(data type)

Pointer integer subtraction

Subtracting integer from pointer works very similar to Pointer integer addition. Subtracting n from pointer decreases pointer value by n * sizeof(data type).


Pointer subtraction

C allows to subtract pointer of like pointer types. 

  1. Subtract integer pointer from integer pointer is valid.
  2. Subtracts character pointer from character pointer is valid.
  3. Subtract character pointer from integer pointer is invalid.
What does the subtraction of pointer result in ?
To answer this i would like to demonstrate it using in example.


#include <stdio.h>                                                              
int main()
{
   int i, j ,k;
   int *pi = &i, *pj = &j, *pk = &k;

   printf("Pointer pi is %p\n", pi);
   printf("Pointer pj is %p\n", pj);
   printf("Pointer pk is %p\n", pk);

   printf("Pointer substraction\n");
   printf("pj - pi is %ld\n", pj - pi);
   printf("pk - pi is %ld\n", pk - pi);

   return 0;
}


Output of above program is 


Pointer pi is 0x7fff67dbce84
Pointer pj is 0x7fff67dbce88
Pointer pk is 0x7fff67dbce8c
Pointer substraction
pj - pi 1
pk - pi 2

Hence subtraction of two integer pointers return number of integer that could occupy between those two pointer. In general we can conclude the difference between two pointer is difference/sizeof of type.

Pointer operator invalids.

Following operation on pointer is invalid
  • Adding two pointer variables.
  • Multiplying two pointer variables.
  • Dividing two pointer variables.
  • Remainder
Pointer arithmetic is used often with array. Which will be covered later.

Links

Next Article - C Programming #43: Pointer - NULL
Previous Article - C Programming #41: Pointer - introduction

All Article - C Programming

No comments :

Post a Comment