hacking contest

hacking exploits security forum
hacking
compliance articles
upgrade backup exec
information security consultant

Full Version: Pointers Question
onlinepass
#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
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
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.

 
Invision Power Board © 2001-2005 Invision Power Services, Inc.