按照Python documentation:
的推荐方法来创建具体数组类型是乘以 具有正整数的任何ctypes的数据类型。或者,您可以 这个类型的子类和定义长度和类型类变量。 可以使用标准下标和 切片存取读取和写入数组元素;对于片段读取而言,结果对象本身不是 阵列。
实施例:
>>> from ctypes import *
>>> TenIntegers = c_int * 10
>>> ii = TenIntegers(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
>>> print ii
<c_long_Array_10 object at 0x...>
>>> for i in ii: print i,
...
1 2 3 4 5 6 7 8 9 10
>>>
因此,第一部分ctypes.c_int * len(x)
创建一个数组类型与len(x)
元素:
In [17]: ctypes.c_int * 10
Out[17]: __main__.c_int_Array_10
In [18]: ctypes.c_int * 100
Out[18]: __main__.c_int_Array_100
类型创建之后,你应该调用它并传递数组元素:
(ctypes.c_int * len(x))(*x)
# ^^^^
创建的数组类型接受元素的可变参数个数,这样,你应该expand list x
using the *x
form:
In [24]: x = [1, 2, 3]
In [25]: (ctypes.c_int * len(x))(*x)
Out[25]: <__main__.c_int_Array_3 at 0x7f0b34171ae8>
In [26]: list((ctypes.c_int * len(x))(*x))
Out[26]: [1, 2, 3]
In [27]: (ctypes.c_int * len(x))(*x)[1]
Out[27]: 2
你不可错过x
,因为__init__
预计整数:
In [28]: (ctypes.c_int * len(x))(x)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-28-ff45cb7481e4> in <module>()
----> 1 (ctypes.c_int * len(x))(x)
TypeError: an integer is required (got type list)
你没有提到你使用的是什么这是有效的,但是你可以使用'sizeof'来获得要传输的字节数。例如:'cx =(ctypes.c_int * len(x))(* x);''num_bytes = ctypes.sizeof(cx)'。 – eryksun
我假设你将数组作为C函数参数传递。这是可行的,因为ctypes数组就像C数组一样,作为指向第一个元素的指针传递。 – eryksun
是的,它被作为C函数参数传递。 – Francis