Tuesday, 21 May 2013

Pointer Arithmetic


All types of arithmetic operations are impossible with a pointer. The only valid operations that can be performed are as-


1. Addition of integer to a pointer and increment operations.

2. Subtraction of an integer from a pointer and decrement operations.

3. Subtraction of a pointer from another pointer of same type
pointer arithmetic is somewhat different from ordinary arithmetic. Here all arithmetic is performed relative to the size of basic type of pointer.

For example, 

If we have an integer pointer pi which contains address 1000, then on incrementing we get 1004 instead of 1001. This is because the size of Pointer is Four depends on architecture.

Rules of the pointer Arithmetic

Rule1.

Address+Number=Address (next address)
Address-Number=Address (pre address)
Address++=Address (next address)
Address--=Address (pre address)
++Address=Address (next address)
--Address=Address (pre address)

p1+p2;Error
p1+1;(next address)
p1++;(next address)
p1=p1+1;
++p1;(next address)
P2-p1;valid(number of element)
p2-1;valid
p2; valid
--p2;

Rule 2:

Addresss-Address=Number(number of elements)

Rule 3.

Address+Address=illegal
Address*Address=illegal
Address/Address=illegal
Address%Address-illegal

Rule4:

We can use relation operator and conditional operator between two pointers.
(<,>,<=,>=,==,!==)
Address>Address=true/false
Address>=Address=true/false


Rule 5:

We can't per from the bit wise between two pointers like
Address&Address=illegal
Address|Address=illegal
Address^Address=illegal
-Address=illegal

Rule6:

We can find size of a pointer using size of the operator.

Example of arithmetic pointers


#include<stdio.h>
main

{
static int arr[]={0,1,2,3,4};
int *p[]={arr,arr+1,arr+2,arr+3,arr+4};
int **ptr=p;
ptr++;
printf("\n%d%d%d,ptr-p,*ptr-arr,**ptr);
*ptr++;
printf("\n %d %d %d",ptr-p,*ptr-arr,**ptr);
*++ptr;
printf("\n %d %d %d",ptr-p,*ptr-arr,**ptr);
++*ptr;
printf("\n %d %d %d",ptr-p,*ptr-arr,**ptr);
}


Output:111
          222
          333
          344

Explanation

Let us consider the arr and the two pointers with the same address.

arr

arr_value        0       1      2        3         4    
arr_address   100   102    104    106      108


P

arr_address       100       102    104   106   108
pointer_address 1000    1002   1004  1006  1008

ptr

p_address  100
ptr_address 200

Ptr is incremented by ptr++ and has value 1002, Now ptr-p is (1002-1000)/(scaling factor)==1,*ptr-arr=(102-100)/(scaling factor)===1,**ptr=1.

Hence the output of the first print is 1,1,1.
*ptr++ becomes 1004. Hence the output for the ptr-p=2,*ptr-arr=2,**ptr=2.
*++ptr becomes 1004. Hence the output for the ptr-p=3,*ptr-arr=3,**ptr=3.
++*ptr increments ptr by the scaling factor. so the value in array p at location 1006 changes from 106 10 108.hence ,the outputs for ptr-p=1006-1000=3,*ptr-arr=108-100=4,**ptr=4.

Example2:

#include<stdio.h>
main()
{
char *str="xyz"
char *ptr=str;
char lest=127;
while(*ptr++)
least =(*ptr<leeast)?*ptr:least;
printf("%d", least);
}


Output 0

Explanation

When ptr reaches the end with the string, the value pointed by ptr is'\0' ,value of ptr is less the value of least. So the logical result of expression is zero. Hence least is zero.


3 comments:

can you post top on pointer to pointer.

Nice post with helpful info.

This website i wonderful

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More