void *字面上是float,如何转换? [英] void* is literally float, how to cast?

查看:2241
本文介绍了void *字面上是float,如何转换?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我在我的C ++应用程序中使用这个C库,其中一个函数返回一个void *。现在我不是纯C的最尖锐的,但听说一个void *可以投射到几乎任何其他*类型。我也知道,我期望在这个函数的某个地方有一个浮点。

So I'm using this C library in my C++ app, and one of the functions returns a void*. Now I'm not the sharpest with pure C, but have heard that a void* can be cast to pretty much any other *-type. I also know that I expect a float at the end somewhere from this function.

所以我将void *转换为float *,并解除引用float *,crash。 darn!
我调试代码,在gdb让它评估(float)voidPtr 和低,看看这个值是我期望和需要!

So I cast the void* to a float* and dereference the float*, crash. darn!. I debug the code and in gdb let it evaluate (float)voidPtr and low and behold the value is what I expect and need!

但是等待,在编译期间是不可能的。如果我写 float number =(float)voidPtr; 它不编译,这是可以理解的。

But wait, it's impossible to the same during compile time. If I write float number = (float)voidPtr; it doesn't compile, which is understandable.

现在的问题,我如何让我的浮动出这个fricking void *?

So now the question, how do I get my float out of this fricking void*?

编辑:感谢H2CO3这被解决了,但我看到很多答案和评论和dissappering不相信我可以做(float)voidPtr在gdb。

Thanks to H2CO3 this was solved, but I see lots of answers and comments appearing and dissappering not believing that I could do (float)voidPtr in gdb. here is the screenshot.

推荐答案

尝试使用指针:

void *theValueAsVoidPtr = // whatever

float flt = *(float *)&theValueAsVoidPtr;

这篇关于void *字面上是float,如何转换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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