C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

floatptr=(float*) intptr;

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
omariqbalnaru@inbox.com
Guest





PostPosted: Sat Aug 26, 2006 8:58 am    Post subject: floatptr=(float*) intptr; Reply with 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?
Back to top
Steve Pope
Guest





PostPosted: Sat Aug 26, 2006 8:58 am    Post subject: Re: floatptr=(float*) intptr; Reply with quote



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





PostPosted: Sat Aug 26, 2006 9:11 am    Post subject: Re: floatptr=(float*) intptr; Reply with quote



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





PostPosted: Sat Aug 26, 2006 9:11 am    Post subject: Re: floatptr=(float*) intptr; Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.