内联浮动到uint"表示"不起作用?

Pod*_*Pod 0 c c++

这是在C中,但我标记它是C++,因为它是相同的.这是用以下内容构建的:Microsoft(R)32位C/C++优化编译器版本14.00.50727.220适用于80x86,如果这有任何不同

为什么这样做?

(inVal是0x80)

float flt = (float) inVal;
outVal = *((unsigned long*)&flt);
Run Code Online (Sandbox Code Playgroud)

(结果outVal为0x43000000 - 正确)

但这不是吗?

outVal = *((unsigned long*)&((float)inVal));
Run Code Online (Sandbox Code Playgroud)

(结果outVal为0x00000080 - NOT CORRECT :()

在问这个问题之前,我用Google搜索了一下,在java中发现了这个函数基本上可以做我想要的.如果你对我正在尝试做的事情感到有点困惑,这个程序可能有助于解释它:

class hello
{
    public static void main(String[] args)
    {
        int inside = Float.floatToIntBits(128.0f);
        System.out.printf("0x%08X", inside);
    }
}
Run Code Online (Sandbox Code Playgroud)

Kon*_*lph 7

你试图获取非const临时的地址((float)转换的结果) - 这在C++中是非法的(也可能在C中).因此,您的代码会导致垃圾.

在您的第一个工作代码中,您没有使用临时代码,因此您的代码正常运行.请注意,从标准的角度来看,这仍然是不明确的,因为未指定所涉及类型的大小和内部表示,并且可能因平台和编译器而异.不过,你可能很安全.