In C, if I cast & dereference a pointer, does it matter which one I do first?

前端 未结 6 1766
南旧
南旧 2020-12-08 05:39

In C, you can cast both simple data types like int, float, and pointers to these.

Now I would have assumed that if you want to convert from

6条回答
  •  清歌不尽
    2020-12-08 06:27

    If you dereference first, cast to int later, you will get the usual (truncation) behavior of casts from float to int. If you cast to pointer to int first, then dereference, the behaviour isn't defined by the standard. Usually it manifests in interpreting the memory that contains the float as int. See http://en.wikipedia.org/wiki/IEEE_754-2008 for how that works out.

提交回复
热议问题