ctypes 浮动垃圾返回

Ser*_*nin 2 python ctypes

当我尝试使用以下命令将 float 加载到 python 程序中时,我遇到了一个问题ctypes

C代码:

float test_ret_float(){
  return 1.0;
}
Run Code Online (Sandbox Code Playgroud)

在Python中,所有的方式都会产生垃圾:

print lib.test_ret_float()
>>1074161254
print c_float(lib.test_ret_float()).value
>>1074161280.0
Run Code Online (Sandbox Code Playgroud)

如果int一切正常的话。

看来,该类型对话没有按预期工作,并且确实返回raw4byte 值,该值未转换为floatbut int,为什么?

Ser*_*nin 5

问题是我错过了restype设置:

lib.test_ret_float.restype = c_float
Run Code Online (Sandbox Code Playgroud)

这样就解决了问题