Pointer Arithmetic

The address is the memory location that is assigned to the variable. We can perform arithmetic operations on the pointers like addition, subtraction etc. There are only a few operations that are allowed to perform on pointers in C. In pointer from pointer subtraction the result will be an integer value. The arithmetic operations are possible on the pointer in C language

  • Increment
  • Decrement
  • Addition
  • Subtraction

Incrementing pointer in C

We using pointer in our program instead of an array because the variable pointer can be incremented.In C it is actually increments by the number equal to the size of the data type.We can traverse the array by using increment operation on pointer which will keep pointing to every element of the array, perform some operation and update itself in a loop.

Rule to increment the pointer is given below

new_address=current_address + i * size_of(data type)

32-bit

For 32-bit int variable it will be incremented by 2 bytes.

64-bit

For 64-bit int variable it will be incremented by 4 bytes.

Let’s see the example of incrementing pointer variable

#include<stdio.h>  
int main(){  
int number=50;        
int *p;     
p=&number;        
printf("Address of p variable is %u \n",p);        
p=p+1;        
printf("After increment: Address of p variable is %u \n",p);       
return 0;  
}

Output

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

Decrement pointer in C

If we are decrement a pointer it will start pointing to the previous location.It is condition that also comes under subtraction.

Rule to the decrement pointer is given below

new_address= current_address – i * size_of(data type)

32-bit

For 32-bit int variable it will be decremented by 2 bytes.

64-bit

For 64-bit int variable it will be decremented by 4 bytes.

Let’s see the example of decremented pointer

#include <stdio.h>            
void main(){            
int number=50;        
int *p;     
p=&number;
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

Submit your review
1
2
3
4
5
Submit
     
Cancel

Create your own review

The Technical Funda
Average rating:  
 0 reviews