|
Subtraction of two pointers: Pointer – Pointer
When we subtract two pointers, we get the number of variables of datatype as same as pointer that are stored between the two pointers.
If we use int pointers:
int *x;
int*y;
int i=1;
int y=2;
int d;
Suppose address of i is 3432 and that of j is 4456 and int requires 2 bytes
x=&I;
y=&j;
d= y –x; // answer get is (4456 – 3432)/2 = 1024/2 = 512.
// we divide by 2 as int takes 2 bytes
If we use char pointers:
char *x;
char*y;
char i;
char y;
char d;
Suppose address of i is 3432 and that of j is 4456 and char requires 1 bytes
x=&I;
y=&j;
d= y –x; // answer get is (4456 – 3432)/1 = 1024/1 = 1024.
// we divide by 1 as char takes 1 bytes
All other types of operations on pointers like addition of pointers, multiplying pointer by number, dividing pointer by number are illegal.
|