Pointer Arithmetic in C

Pointer Arithmetic in C

  • In C pointer holds address of a value, so there can be arithmetic operations on the pointer variable. Following arithmetic operations are possible on pointer in C language:
  1. Increment.
  2. Decrement.
  3. Addition.
  4. Subtraction.
  5. Comparison.


Program for pointer arithmetic(32-bit machine)

#include <stdio.h>
int main()
{    
int m = 5, n = 10, o = 0;
    int *p1;    int *p2;    int *p3;
    
p1 = &m;    //printing the address of m    
p2 = &n;    //printing the address of n
    
printf("p1 = %d\n", p1);    
printf("p2 = %d\n", p2);
    o = *p1+*p2;    
printf("*p1+*p2 = %d\n", o);//point 1
    
p3 = p1-p2;    
printf("p1 - p2 = %d\n", p3); //point 2
    p1++;    
printf("p1++ = %d\n", p1); //point 3
    p2--;    
printf("p2-- = %d\n", p2); //point 4
    
//Below line will give ERROR    
printf("p1+p2 = %d\n", p1+p2); //point 5
    return 0;
}
p1 = 2680016p2 = 2680012*p1+*p2 = 15p1-p2 = 1p1++ = 2680020p2-- = 2680008

Explanation of the above program:
  • Here, * means 'value at the given address'. Thus, it adds the value of m and n which is 15.
  • It subtracts the addresses of the two variables and then divides it by the size of the pointer datatype (here integer, which has size of 4 bytes) which gives us the number of elements of integer data type that can be stored within it.
  • It increments the address stored by the pointer by the size of its datatype(here 4).
  • It decrements the address stored by the pointer by the size of its datatype(here 4).
  • Addition of two pointers is not allowed.

Incrementing Pointer in C

  • Incrementing a pointer is used in array because it is contiguous memory location. Moreover, we know the value of next location.
  • Increment operation depends on the data type of the pointer variable. The formula of incrementing pointer is given below:
  • new_address= current_address + i * size_of(data type)
#include <stdio.h>   
#include<conio.h>       
void main(){          
int number=50;      
int *p;//pointer to int    
p=&number;//stores the address of number variable      
printf("Address of p variable is %u \n",p);      
p=p+1;     
printf("After increment: Address of p variable is %u \n",p);      

  
Output
Address of p variable is 3214864300 
After increment: Address of p variable is 3214864304


Decrementing Pointer in C
new_address= current_address - i * size_of(data type)

#include <stdio.h>       
#include <conio.h>        
void main(){          
int number=50;      
int *p;//pointer to int    
p=&number;//stores the address of number variable      
printf("Address of p variable is %u \n",p);      
p=p-1;     
printf("After decrement: Address of p variable is %u \n",p);      
}  
Output 
Address of p variable is 3214864300 
 After decrement: Address of p variable is 3214864296    

#include <stdio.h>
const int MAX = 3;
int main ()
{
   int  var[] = {10, 100, 200};
   int  i, *ptr;
   /* let us have array address in pointer */
   ptr = &var[MAX-1];
   for ( i = MAX; i > 0; i--) {
      printf("Address of var[%d] = %x\n", i-1, ptr );
      printf("Value of var[%d] = %d\n", i-1, *ptr );
      /* move to the previous location */
      ptr--;
   }
   return 0;
}

Expected Output

Address of var[2] = bfedbcd8
Value of var[2] = 200
Address of var[1] = bfedbcd4
Value of var[1] = 100
Address of var[0] = bfedbcd0
Value of var[0] = 10


Incrementing a Pointer
#include <stdio.h>
const int MAX = 3;
int main () {
   int  var[] = {20, 200, 400};
   int  i, *ptr;
   /* let us have array address in pointer */
   ptr = var;
   for ( i = 0; i < MAX; i++) {
      printf("Address of var[%d] = %x\n", i, ptr );
      printf("Value of var[%d] = %d\n", i, *ptr );
      /* move to the next location */
      ptr++;
   }
   return 0;
}

Expected Output
Address of var[0] = bf882b30
Value of var[0] = 20
Address of var[1] = bf882b34
Value of var[1] = 200
Address of var[2] = bf882b38
Value of var[2] = 400


Post a Comment

Previous Post Next Post