I have a program question, here is the code.
int main()
{
int *p,*q;
p=(int*)1000;
printf("%d ",p);
q=(int*)2000;
printf("%d",q);
printf("%d",(p-q));
return 0;
}
But answer coming as
1000
2000
-250
I am unable to understand what happen with p-q and why answer came as -250?
Correct but probably useless answer:
p - qis equal to(1000 - 2000) / (sizeof int). For most C compiles,sizeof intis 4.Potentially more useful answer: the effect of typecasts like
(int*) 1000is undefined. That code creates a pointer to an int at address 1000. That address is probably invalid. To create a pointer to an int with value 1000, write this:Now
ppoints toi, and*p, the value pointed to byp, is 1000.Here is some correct code that may say what you meant: