# Pointers Arithmatic Operations in C

Pointer variables are used to store address of variables. Address of any variable is an unsigned integer value i.e., it is a numerical value. So we can perform arithematic operations on pointer values. But when we perform arithematic operations on pointer variable, result depends on the amount of memory required by the variable to which the pointer is pointing.

In c programming language, we can perform the following arithematic operations on pointers...

1. Addition
2. Subtraction
3. Increment
4. Decrement
5. Comparison

Addition Operation on Pointer

In c programming language, the addition operation on pointer variables is calculated using the following formula...

AddressAtPointer + ( NumberToBeAdd * BytesOfMemoryRequiredByDatatype )

Example

``````#include<stdio.h>
#include<conio.h>

void main()
{
int a, *intPtr ;
float b, *floatPtr ;
double c, *doublePtr ;
clrscr() ;
intPtr = &a ;  // Asume address of a is 1000
floatPtr = &b ;  // Asume address of b is 2000
doublePtr = &c ;  // Asume address of c is 3000

intPtr = intPtr + 3 ;  // intPtr = 1000 + ( 3 * 2 )
floatPtr = floatPtr + 2 ;  // floatPtr = 2000 + ( 2 * 4 )
doublePtr = doublePtr + 5 ;  // doublePtr = 3000 + ( 5 * 6 )

printf("intPtr value : %u\n", intPtr) ;
printf("floatPtr value : %u\n", floatPtr) ;
printf("doublePtr value : %u", doublePtr) ;

getch() ;
}
``````

intPtr value : 1006
floatPtr value : 2008
doublePtr value : 3030

Subtraction Operation on Pointer

In c programming language, the subtraction operation on pointer variables is calculated using the following formula...

AddressAtPointer - ( NumberToBeAdd * BytesOfMemoryRequiredByDatatype )

Example

``````#include<stdio.h>
#include<conio.h>

void main()
{
int a, *intPtr ;
float b, *floatPtr ;
double c, *doublePtr ;
clrscr() ;
intPtr = &a ;  // Asume address of a is 1000
floatPtr = &b ;  // Asume address of b is 2000
doublePtr = &c ;  // Asume address of c is 3000

intPtr = intPtr - 3 ;  // intPtr = 1000 - ( 3 * 2 )
floatPtr = floatPtr - 2 ;  // floatPtr = 2000 - ( 2 * 4 )
doublePtr = doublePtr - 5 ;  // doublePtr = 3000 - ( 5 * 6 )

printf("intPtr value : %u\n", intPtr) ;
printf("floatPtr value : %u\n", floatPtr) ;
printf("doublePtr value : %u", doublePtr) ;

getch() ;
}
``````

intPtr value : 994
floatPtr value : 1992
doublePtr value : 2970

Increment & Decrement Operation on Pointer

The increment operation on pointer variable is calculated as follows...

AddressAtPointer + NumberOfBytesRequiresByDatatype

Example

``````#include<stdio.h>
#include<conio.h>

void main()
{
int a, *intPtr ;
float b, *floatPtr ;
double c, *doublePtr ;
clrscr() ;
intPtr = &a ;  // Asume address of a is 1000
floatPtr = &b ;  // Asume address of b is 2000
doublePtr = &c ;  // Asume address of c is 3000

intPtr++ ;  // intPtr = 1000 + 2
floatPtr++ ;  // floatPtr = 2000 + 4
doublePtr++ ;  // doublePtr = 3000 + 6

printf("intPtr value : %u\n", intPtr) ;
printf("floatPtr value : %u\n", floatPtr) ;
printf("doublePtr value : %u", doublePtr) ;

getch() ;
}
``````

intPtr value : 1002
floatPtr value : 2004
doublePtr value : 3006

The decrement operation on pointer variable is calculated as follows...

AddressAtPointer - NumberOfBytesRequiresByDatatype

Example

``````#include<stdio.h>
#include<conio.h>

void main()
{
int a, *intPtr ;
float b, *floatPtr ;
double c, *doublePtr ;
clrscr() ;
intPtr = &a ;  // Asume address of a is 1000
floatPtr = &b ;  // Asume address of b is 2000
doublePtr = &c ;  // Asume address of c is 3000

intPtr-- ;  // intPtr = 1000 - 2
floatPtr-- ;  // floatPtr = 2000 - 4
doublePtr-- ;  // doublePtr = 3000 - 6

printf("intPtr value : %u\n", intPtr) ;
printf("floatPtr value : %u\n", floatPtr) ;
printf("doublePtr value : %u", doublePtr) ;

getch() ;
}
``````

intPtr value : 998
floatPtr value : 1996
doublePtr value : 2994

Comparison of Pointers

The comparison operation is perform between the pointers of same datatype only. In c programming language, we can use all comparison operators (relational operators) with pointers.

We can't perform multiplication and division operations on pointers.