1 回答
TA贡献1786条经验 获得超11个赞
您的nofentry值是一个指向float数组的指针,而generate_codebook需要一个指向float的指针。ctypes的不能自动执行这样的转换,所以它必须被手动执行(使用[Python的3]:ctypes的铸造(OBJ,类型))。
例子:
>>> import ctypes
>>>
>>> dim = 100
>>>
>>> FloatArr100 = ctypes.c_float * dim
>>> FloatArr100Ptr = ctypes.POINTER(FloatArr100)
>>>
>>> float_arr = FloatArr100(*range(dim))
>>> float_arr[4], float_arr[38], float_arr[99]
(4.0, 38.0, 99.0)
>>>
>>> float_arr_ptr = ctypes.pointer(float_arr) # This is the equivalent of your `nofentry`
>>> float_arr_ptr
<__main__.LP_c_float_Array_100 object at 0x000001921ED85A48>
>>> type(float_arr_ptr) is FloatArr100Ptr
True
>>>
>>> float_ptr = ctypes.cast(float_arr, ctypes.POINTER(ctypes.c_float)) # This is what you should do
>>>
>>> float_ptr
<__main__.LP_c_float object at 0x000001921ED859C8>
>>> float_ptr[4], float_ptr[38], float_ptr[99]
(4.0, 38.0, 99.0)
翻译成你的代码:
将nofentry定义更改为:
nofentry = (ctypes.c_float * (len(nofobservations) * nofterminal * terminal.shape[1]))() # Notice dropping `ctypes.POINTER`
调用时mydll.generate_codebook,替换
ctypes.byref(nofentry)
和
ctypes.cast(nofentry, ctypes.POINTER(ctypes.c_float))
所以最后它看起来像:
result = mydll.generate_codebook(nodestatus.ctypes.data_as(ctypes.POINTER(ctypes.c_int)),
nofnode, noftree, terminal.ctypes.data_as(ctypes.POINTER(ctypes.c_int)),
c_int(nofterminal),
nofobservations.ctypes.data_as(ctypes.POINTER(ctypes.c_int)),
total, nofseries,
ctypes.cast(nofentry, ctypes.POINTER(ctypes.c_float)))
添加回答
举报