floatptr=(float*) intptr;

O

omariqbalnaru

#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?
 
I

Ian Collins

#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?
 
S

Steve Pope

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
 
I

Ian Collins

Steve said:
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.
 
S

Steve Pope

Ian Collins said:
Steve Pope wrote:
It's worse than that, int and float could have different sizes, on a 16
bit platform for example.

Right, my statement above assumes 32 bit int's and floats which
while common is not a given.

Steve
 
O

Old Wolf

That isn't the exact text of the OP, where I'm reading .. did he
post a second message that I didn't get, or did you alter it?
From a language standpoint it is undefined,

Only if "pi" is not correctly aligned for (float *).
from a practical standpoint you are creating a pointer to a
32-bit single-precision IEEE floating point value
From a theoretical standpoint, it may not be 32 bits and it
may not be IEEE.
whose bit pattern is equal to that of the 32-bit 2's
complement integer pointed to by pi.

Note - your "whose" refers to the pointed-to value
(initially I read your statement as "whose" referring
to the pointer).

To clarify: the posted code sets the pointer-to-float to
point to the same memory location as the pointer-to-int
is currently pointing to.

(Assuming that "pi" is in fact a pointer to int, which the
original poster didn't specify).

His program doesn't print out a value. As long as
alignment is correct, the cast is entirely legal and
well-defined. You would only get into trouble if you
went on to read the pointed-to value via "pi".
 
S

Steve Pope

Old Wolf said:
Steve Pope wrote:
That isn't the exact text of the OP, where I'm reading .. did he
post a second message that I didn't get, or did you alter it?

He posted twice, once with the above question, and once with
the same question reworded.
Note - your "whose" refers to the pointed-to value
(initially I read your statement as "whose" referring
to the pointer).

Okay, my sentence could be ambiguous.
To clarify: the posted code sets the pointer-to-float to
point to the same memory location as the pointer-to-int
is currently pointing to.

(Assuming that "pi" is in fact a pointer to int, which the
original poster didn't specify).
His program doesn't print out a value.

Maybe you are not seeing one of his two posts?

Steve
 
M

Michiel.Salters

Steve said:
Right, my statement above assumes 32 bit int's and floats which
while common is not a given.

Also, your statement assumes the alignment of float and int is the
same.

To the OP: the cast means "I know *exactly* what I'm doing". Since you
have to ask, you haven't reached that familiarity with C++ yet. So
avoid
these things until you can debug the errors it will give you.

HTH,
Michiel Salters
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top