Ctypes上的数组

时间:2016-07-05 03:18:24

标签: python python-2.7 ctypes

有什么区别:

c_int(24)
24 * c_int
(24 * c_int)()
(24 * c_int)

>>>c_int(24) == 24 * c_int
False
>>> 24 * c_int == (24 * c_int)
True
>>> 24 * c_int == (24 * c_int)()
False

当我问出差异时,它意味着上面的选项是24个整数元素的真实数组

1 个答案:

答案 0 :(得分:0)

>>> from ctypes import *
>>> c_int(24) # Create instance of c_int (c_long is an alias)
c_long(24)

>>> 24 * c_int # Creates an 24-element array type (NOT an instance)
<class '__main__.c_long_Array_24'>

>>> (24 * c_int)() # Creates an instance of a 24-element array.
<__main__.c_long_Array_24 object at 0x00000000029871C8>

>>> (24 * c_int)  # Same as second one.  Creates type.
<class '__main__.c_long_Array_24'>

所以第三个为24个整数元素创建真正的数组。