c语言中len的作用,(ctypes.c_int*len(x))(*x)做什么?

The recommended way to create concrete array types is by multiplying

any ctypes data type with a positive integer. Alternatively, you can

subclass this type and define length and type class variables.

Array elements can be read and written using standard subscript and

slice accesses; for slice reads, the resulting object is not itself an

Array.

Example:>>> from ctypes import *

>>> TenIntegers = c_int * 10

>>> ii = TenIntegers(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)

>>> print ii

>>> 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

创建类型后,应调用它并传递数组元素:

^{pr2}$

In [24]: x = [1, 2, 3]

In [25]: (ctypes.c_int * len(x))(*x)

Out[25]: <__main__.c_int_array_3 at>

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)

in ()

> 1 (ctypes.c_int * len(x))(x)

TypeError: an integer is required (got type list)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值