Pointer Arithmetic
Prepared By: Mr. S. A. Patil
Assistant Professor,PVPIT Budhgaon
Pointer Arithmetic
 A pointer in c is an address, which is a numeric value.
 Therefore, we can perform arithmetic operations on a pointer just as you can on a
numeric value.
 A limited set of arithmetic operations can be performed on pointers. A pointer may
be:
1. Incremented ( ++ )
2. Decremented ( — )
3. An integer may be added to a pointer ( + or += )
4. An integer may be subtracted from a pointer ( – or -= )
 Pointers contain addresses. Adding two addresses makes no sense, because there
is no idea what it would point to.
 Subtracting two addresses lets you compute the offset between these two
addresses.
Incrementing a Pointer
 If we increment a pointer by 1, the pointer will start pointing to the immediate next
location.
 This is different from the general arithmetic because the value of the pointer will
get increased by the size of the data type to which the pointer is pointing.
 The Rule to increment the pointer is given below:
new_address= current_address + i * size_of(data type)
 Where i is the number by which the pointer get increased.
 We can traverse an array by using pointer.
Incrementing a Pointer
Example:
int a=10;
int *p;
p=&a;
p=p+1;
Here suppose address of a is 2010 which is stored in pointer variable p;
We perform increment operation on pointer variable p. then new value of p becomes.
p=p+1*2=2010+2=2012
Decrementing Pointer in C
 Like increment, we can decrement a pointer variable. If we decrement a pointer, it
will start pointing to the previous location. The formula of decrementing the
pointer is given below:
new_address= current_address - i * size_of(data type)
Example:
int a=10;
int *p;
p=&a;
p=p-1;
Here suppose address of a is 2010 which is stored in pointer variable p;
We perform decrement operation on pointer variable p. then new value of p becomes.
p=p-1*2=2010-2=2012
Pointer Addition
 We can add a value to the pointer variable. The formula of adding value to
pointer is given below:
new_address= current_address + (number * size_of(data type))
Example:
float a=10;
float *p;
p=&a;
p=p+3;
Here suppose address of a is 2410 which is stored in pointer variable p;
We add value 3 in pointer variable p. then new value of p becomes the address.
p=p+(3*4)=2410+12=2422
 We can not add two pointers in each other
Pointer Subtraction
 Like pointer addition, we can subtract a value from the pointer variable.
 Subtracting any number from a pointer will give an address.
 The formula of subtracting value from the pointer variable is given below:
new_address= current_address - (number * size_of(data type))
Example:
float a=10;
float *p;
p=&a;
p=p-2;
Here suppose address of a is 2410 which is stored in pointer variable p;
We subtract value 2 from pointer variable p. then new value of p becomes the address.
p=p-(2*2)=2410-4=2406
 We can subtract two pointers from each other to get offset.
Illegal arithmetic with pointers
 Address + Address = illegal
 Address * Address = illegal
 Address % Address = illegal
 Address / Address = illegal
 Address & Address = illegal
 Address ^ Address = illegal
 Address | Address = illegal
 ~Address = illegal

Pointer arithmetic in c

  • 1.
    Pointer Arithmetic Prepared By:Mr. S. A. Patil Assistant Professor,PVPIT Budhgaon
  • 2.
    Pointer Arithmetic  Apointer in c is an address, which is a numeric value.  Therefore, we can perform arithmetic operations on a pointer just as you can on a numeric value.  A limited set of arithmetic operations can be performed on pointers. A pointer may be: 1. Incremented ( ++ ) 2. Decremented ( — ) 3. An integer may be added to a pointer ( + or += ) 4. An integer may be subtracted from a pointer ( – or -= )  Pointers contain addresses. Adding two addresses makes no sense, because there is no idea what it would point to.  Subtracting two addresses lets you compute the offset between these two addresses.
  • 3.
    Incrementing a Pointer If we increment a pointer by 1, the pointer will start pointing to the immediate next location.  This is different from the general arithmetic because the value of the pointer will get increased by the size of the data type to which the pointer is pointing.  The Rule to increment the pointer is given below: new_address= current_address + i * size_of(data type)  Where i is the number by which the pointer get increased.  We can traverse an array by using pointer.
  • 4.
    Incrementing a Pointer Example: inta=10; int *p; p=&a; p=p+1; Here suppose address of a is 2010 which is stored in pointer variable p; We perform increment operation on pointer variable p. then new value of p becomes. p=p+1*2=2010+2=2012
  • 5.
    Decrementing Pointer inC  Like increment, we can decrement a pointer variable. If we decrement a pointer, it will start pointing to the previous location. The formula of decrementing the pointer is given below: new_address= current_address - i * size_of(data type) Example: int a=10; int *p; p=&a; p=p-1; Here suppose address of a is 2010 which is stored in pointer variable p; We perform decrement operation on pointer variable p. then new value of p becomes. p=p-1*2=2010-2=2012
  • 6.
    Pointer Addition  Wecan add a value to the pointer variable. The formula of adding value to pointer is given below: new_address= current_address + (number * size_of(data type)) Example: float a=10; float *p; p=&a; p=p+3; Here suppose address of a is 2410 which is stored in pointer variable p; We add value 3 in pointer variable p. then new value of p becomes the address. p=p+(3*4)=2410+12=2422  We can not add two pointers in each other
  • 7.
    Pointer Subtraction  Likepointer addition, we can subtract a value from the pointer variable.  Subtracting any number from a pointer will give an address.  The formula of subtracting value from the pointer variable is given below: new_address= current_address - (number * size_of(data type)) Example: float a=10; float *p; p=&a; p=p-2; Here suppose address of a is 2410 which is stored in pointer variable p; We subtract value 2 from pointer variable p. then new value of p becomes the address. p=p-(2*2)=2410-4=2406  We can subtract two pointers from each other to get offset.
  • 8.
    Illegal arithmetic withpointers  Address + Address = illegal  Address * Address = illegal  Address % Address = illegal  Address / Address = illegal  Address & Address = illegal  Address ^ Address = illegal  Address | Address = illegal  ~Address = illegal