What happens when float pointer is typecasted to char pointer?

前端 未结 6 1408
后悔当初
后悔当初 2020-12-12 04:09
int main()
{
    float f = 12.2;
    char *p1;
    p1 = (char *)&f;
    printf (\"%d\", *p1);
}

This outputs 51.

6条回答
  •  借酒劲吻你
    2020-12-12 04:43

    1. You cast to (char) instead of (char*).
    2. You print it out as integer.
    3. Thus you get the least significant byte of f's address.

    EDIT: According to the new markup you really truncate the float representation to its least significant byte (on little-endian machines)

提交回复
热议问题