| View previous topic :: View next topic |
| Author |
Message |
omariqbalnaru@inbox.com Guest
|
Posted: Sat Aug 26, 2006 8:58 am Post subject: floatptr=(float*) intptr; |
|
|
#include<iostream>
using namespace std;
int main()
{
int i, *pi;
float f, *pf;
i = 1024;
pi = &i;
pf = (float *) pi;
f = *pf;
cout << " i = " << i << " f = " << f << "\n \n";
f = i;
cout << " i = " << i << " f = " << f << "\n \n";
return 0;
}
Can anyone please explain to me what is happening in the
pf = (float *) pi;
statement? |
|
| Back to top |
|
 |
Steve Pope Guest
|
Posted: Sat Aug 26, 2006 8:58 am Post subject: Re: floatptr=(float*) intptr; |
|
|
omariqbalnaru (AT) inbox (DOT) com <omariqbalnaru (AT) inbox (DOT) com> wrote:
| Quote: | Can anyone please explain to me what is happening in the
pf = (float *) pi;
statement?
|
From a language standpoint it is undefined, from a practical
standpoint you are creating a pointer to a 32-bit single-precision
IEEE floating point value whose bit pattern is equal to that of
the 32-bit 2's complement integer pointed to by pi.
Try looking here: http://en.wikipedia.org/wiki/IEEE_754
You may even be able to confirm that the random-looking number
your program is printing out is what is expected from the IEEE
standard. But if so you have even more free time on your hands
than I do. ;)
Steve |
|
| Back to top |
|
 |
Ian Collins Guest
|
Posted: Sat Aug 26, 2006 9:11 am Post subject: Re: floatptr=(float*) intptr; |
|
|
Steve Pope wrote:
| Quote: | omariqbalnaru (AT) inbox (DOT) com <omariqbalnaru (AT) inbox (DOT) com> wrote:
Can anyone please explain to me what is happening in the
pf = (float *) pi;
statement?
From a language standpoint it is undefined, from a practical
standpoint you are creating a pointer to a 32-bit single-precision
IEEE floating point value whose bit pattern is equal to that of
the 32-bit 2's complement integer pointed to by pi.
It's worse than that, int and float could have different sizes, on a 16 |
bit platform for example.
--
Ian Collins. |
|
| Back to top |
|
 |
Ian Collins Guest
|
Posted: Sat Aug 26, 2006 9:11 am Post subject: Re: floatptr=(float*) intptr; |
|
|
omariqbalnaru (AT) inbox (DOT) com wrote:
| Quote: | #include<iostream
using namespace std;
int main()
{
int i, *pi;
float f, *pf;
i = 1024;
pi = &i;
pf = (float *) pi;
f = *pf;
cout << " i = " << i << " f = " << f << "\n \n";
f = i;
cout << " i = " << i << " f = " << f << "\n \n";
return 0;
}
Can anyone please explain to me what is happening in the
pf = (float *) pi;
statement?
Undefined behaviour? |
--
Ian Collins. |
|
| Back to top |
|
 |
|