当浮动指针类型强制转换为字符指针会发生什么? [英] What happens when float pointer is typecasted to char pointer?

查看:117
本文介绍了当浮动指针类型强制转换为字符指针会发生什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

int main()
{
    float f = 12.2;
    char *p1;
    p1 = (char *)&f;
    printf ("%d", *p1);
}

这51输出

推荐答案

可以的的一个浮法* 的char * 就好了,它的的使用的,这样可能会产生问题兽。

You can cast a float* to a char* just fine, it's the using of such a beast that may be problematic.

当你去引用它,你会简单地获得字符重的第一部分presentation(见下文明白这究竟是怎样,它不是浮动的,你可能会认为清晰)。

When you de-reference it, you'll simply get the char representation of the first part (but see below to understand what this really means, it's not as clear as you may think) of the float.

如果你在谈论IEE754花车,12.2 IEEE754浮点是(ABCD是个字节):

If you're talking about IEE754 floats, 12.2 in IEEE754 float is (abcd are the octets):

S EEEEEEEE MMMMMMMMMMMMMMMMMMMMMMM (sign, exponent, mantissa).
0 10000010 10000110011001100110011
a aaaaaaab bbbbbbbccccccccdddddddd

00110011 末是51(0x33),您所看到的。你看到的原因的最后的浮子位是因为它是存储这样的内存(在little-endian的架构):

The 00110011 at the end is the 51 (0x33) that you're seeing. The reason you're seeing the last bit of the float is because it's stored like this in memory (in a little-endian architecture):

00110011 00110011 01000011 01000001
dddddddd cccccccc bbbbbbbb aaaaaaaa

这意味着浮动*的的char * 将在该点 DDDDDDDD 部分。

在大端架构,你会得到 AAAAAAAA 位, 01000001 ,或65(0×41)。

On big-endian architectures, you would get the aaaaaaaa bit, 01000001, or 65 (0x41).

这篇关于当浮动指针类型强制转换为字符指针会发生什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆