a pointer question

take a look at this program

int main()
{
int i, *pi;
float f, *pf;

i=1024;
pi=&i;

f=1024;
pf=&f;

cout << i << " " << f << endl;

pf=(float*)pi;

f=*pf;

cout << i << " " << f << endl;

return 0;
}

Output is

1024 1024
1024 -1.44e14(some junk value)

Can someone explain to me that why is there a junk value in 'f' after again being assigned by pointer 'pf'? and what is assigned to the pointer 'pf' by 'pi'?
Last edited on
Because integers and floats are stored in completely different formats.
Taking the address of an integer and pretending that a floating point number is strored there
(which is really what this cast pf=(float*)pi; is doing), and trying to print is as a float
will obviously produce rubbish.
Topic archived. No new replies allowed.