onlinepass
Jan 22 2004, 10:06 AM
#include<stdio.h>
main()
{
int *p,*q;
p=(int *)1000;
q=(int *)2000;
printf("q-p : %d",(q-p));
return 0;
}
what the output will be and how????
nulladd
Jan 23 2004, 01:00 AM
output:
q-p : 250
reason:
pointer subtraction, as p and q are memory addresses, it gives u the number of objects between q and p and the object that p points to.
these 2 pointers by themselves dont mean much, that is why the output also doesnt mean much, but a practical use of this is in arrays.
example
| CODE |
int a[10]; //some array int *p,*q; //our 2 pointers p=&a[5]; //sets p to the memory address of the 6th element in a q=&a[2]; //sets q to the memory address of the 3rd element in a
|
output of this example is 3, which is the no. of indexes element 5 is from element 2
also note that if you want to get the integer values this is one way to access them
| CODE |
| printf("q-p : %d\n",(int&)q-(int&)p); |
will give you the answer 1000 if you wanted to do integer subtraction