#include <stdio.h>
int main()
{
int *p = (int*) 60; --- Line 1
int *q = (int*) 40; --- Line 2
printf("%d", p-q); //Output is 5
return 0;
}
Could anybody please explain to me the output of this program?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
It means the (implementation-defined) action of assigning an integral value to a pointer happens. This often means that
ppoints to the memory address at60andqto the address at40. These memory addresses could be in virtual memory, hardware memory, and many implementations have different conversion routines for these.Since this is implementation-defined anything could happen, as described by your implementation.
But isn’t this entirely worthless?
It’s most certainly not, it is used a lot in embedded hardware programming to access certain features or call built-in functions.
Most likely on your system
intis 4 bytes wide, sop - qequals(60 - 40) / 4 == 5.