我有这个python指针声明:
numberOfThings = ctypes.c_int32()
indexes = ctypes.c_int32()
如果我打印值:
print numberOfThings.value
print indexes.value
我得到了:
0
0
我执行函数调用:
the_Dll.doSomeThing(ctypes.byref(numberOfThings), ctypes.byref(indexes))
如果我现在打印值:
print "numberOfThings values", numberOfThings.value
print "indexes values", indexes.value
我得到了:
numberOfThings values 3
indexes values 147107816
索引是指向指针的指针,但是如何获取索引的值?我的目标是循环遍历numberOfThings的nr,并为每个numberOfThings打印索引的值,如下所示:
for i in range(numberOfThings.value):
print 'Value of indexes', how to access indexes real value?
有谁知道如何解决这个问题?谢谢!
答案 0 :(得分:1)
来自python的official documentation:
通过在ctypes类型上调用pointer()函数来创建指针实例:
>>>
>>> from ctypes import *
>>> i = c_int(42)
>>> pi = pointer(i)
>>>
指针实例有一个内容属性,该属性返回指针指向的对象,即上面的i对象:
>>>
>>> pi.contents
c_int(42)
>>>
因此,如果我理解你的问题,你应该:
pointer
代替 int32
类型variable.contents
属性访问指针内容(a.k.a。解除引用指针)。跟进我们上次的评论,这里是您可能的解决方案的格式化版本:
numberOfThings = ctypes.c_int32()
indexes = ctypes.c_void_p()
_Dll.doSomeThing(ctypes.byref(numberOfThings),ctypes.byref(indexes))
ptrt = ctypes.POINTER(ctypes.c_double*numberOfThings.value)
mydblPtr = cast(indexes, ctypes.POINTER(ctypes.c_double*numberOfThings.value))
indexes = ptrt(mydblPtr.contents)
for content in indices.contents:
print content
答案 1 :(得分:0)
我认为我们可以从指针而不是int32开始:
numberOfThings = ctypes.c_int32()
indices = ctypes.c_void_p()
_Dll.doSomeThing(ctypes.byref(numberOfThings), ctypes.byref(indices))
然后我们可以将地址转换为正确类型的指向数组的指针(这里假设是双精度数):
ptrt = ctypes.POINTER(ctypes.c_double*numberOfThings.value)
indices = ptrt(indices.contents)
for n in indices.contents:
print n