Wednesday, February 4, 2015

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:
  • Increment
  • Decrement
  • Addition
  • Subtraction
  • Comparison

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:
  1. new_address= current_address + i * size_of(data type)  

32 bit

For 32 bit int variable, it will increment to 2 byte.

64 bit

For 64 bit int variable, it will increment to 4 byte.
Let's see the example of incrementing pointer variable on 64 bit OS.
  1. #include <stdio.h>          
  2. void main(){          
  3. int number=50;      
  4. int *p;//pointer to int    
  5. p=&number;//stores the address of number variable      
  6.           
  7. printf("Address of p variable is %u \n",p);      
  8. p=p+1;     
  9. printf("After increment: Address of p variable is %u \n",p);      
  10. }    

Output

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

Decrementing Pointer in C

Like increment, we can decrement a pointer variable. The formula of decrementing pointer is given below:
  1. new_address= current_address - i * size_of(data type)  

32 bit

For 32 bit int variable, it will decrement to 2 byte.

64 bit

For 64 bit int variable, it will decrement to 4 byte.
Let's see the example of decrementing pointer variable on 64 bit OS.
  1. #include <stdio.h>          
  2. void main(){          
  3. int number=50;      
  4. int *p;//pointer to int    
  5. p=&number;//stores the address of number variable      
  6.           
  7. printf("Address of p variable is %u \n",p);      
  8. p=p-1;     
  9. printf("After decrement: Address of p variable is %u \n",p);      
  10. }    

Output

Address of p variable is 3214864300
After decrement: Address of p variable is 3214864296

C Pointer Addition

We can add a value to the pointer variable. The formula of adding value to pointer is given below:
  1. new_address= current_address + (number * size_of(data type))  

32 bit

For 32 bit int variable, it will add 2 * number.

64 bit

For 64 bit int variable, it will add 4 * number.
Let's see the example of adding value to pointer variable on 64 bit OS.
  1. #include <stdio.h>          
  2. void main(){          
  3. int number=50;      
  4. int *p;//pointer to int    
  5. p=&number;//stores the address of number variable      
  6.           
  7. printf("Address of p variable is %u \n",p);      
  8. p=p+3;   //adding 3 to pointer variable  
  9. printf("After adding 3: Address of p variable is %u \n",p);      
  10. }    

Output

Address of p variable is 3214864300
After adding 3: Address of p variable is 3214864312
As you can see, address of p is 3214864300. But after adding 3 with p variable, it is 3214864312 i.e. 4*3=12 increment. Since we are using 64 bit OS, it increments 12. But if we were using 32 bit OS, it were incrementing to 6 only i.e. 2*3=6. As integer value occupies 2 byte memory in 32 bit OS.

C Pointer Subtraction

Like pointer addition, we can subtract a value from the pointer variable. The formula of subtracting value from pointer variable is given below:
  1. new_address= current_address - (number * size_of(data type))  

32 bit

For 32 bit int variable, it will subtract 2 * number.

64 bit

For 64 bit int variable, it will subtract 4 * number.
Let's see the example of subtracting value from pointer variable on 64 bit OS.
  1. #include <stdio.h>          
  2. void main(){          
  3. int number=50;      
  4. int *p;//pointer to int    
  5. p=&number;//stores the address of number variable      
  6.           
  7. printf("Address of p variable is %u \n",p);      
  8. p=p-3; //subtracting 3 from pointer variable  
  9. printf("After subtracting 3: Address of p variable is %u \n",p);      
  10. }    

Output

Address of p variable is 3214864300
After subtracting 3: Address of p variable is 3214864288
You can see after subtracting 3 from pointer variable, it is 12 (4*3) less than the previous address value.

No comments:

Post a Comment