简体   繁体   English

C指针类型转换

[英]C pointer type cast

float fNum = 12.3;
int *pF = (int *)&fNum;
printf("fNum:%f pF:%d \n", fNum, *pF);
*pF = 233;
printf("fNum:%f pF:%d \n", fNum, *pF);

return 0;

The result is like this 结果是这样的

fNum:12.300000 pF:1095027917 
fNum:0.000000 pF:233 

I want to know why the result like this? 我想知道为什么这样的结果? Don't fNum & *pf point to the same memory? fNum*pf指向相同的内存?

To start with, the variable fNum doesn't point to anything. 首先,变量fNum指向任何东西。 It's the actual value. 这是实际值。

And a floating point number is not the same as an integer. 而浮点数是一样的整数。 The bit-patterns in memory are very different for a floating point number. 对于浮点数,内存中的位模式非常不同。

fNum contains floating point binary presentation of 12.3 . fNum包含12.3 浮点二进制表示形式。 So when your print the content of the fNum memory as integer you will get the translation this binary set as decimal value 因此,当您将fNum内存的内容打印为整数时,您将获得此二进制数的转换为十进制值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM