错误类型:需要 range() 整数结束参数,得到 LP_c_ulong

Error type: range() integer end argument expected, got LP_c_ulong

当我尝试使用 'slots_num' 作为 range():

中的参数时出现此错误
        slots_num = pointer(c_uint32())
        slots = pointer(c_uint32())

        if self.mgetBusSlotsFunc(self.mf, slots_num, slots) != 0:
            raise Exception("Failed to get slots")

        print(devAddr)

        for x in range(0, slots_num):
            print(slots[x])

我做错了什么?

range built-in function expects integers as its arguments

The arguments to the range constructor must be integers (either built-in int or any object that implements the index special method).

但您提供了一个 pointer 实例。

>>> import ctypes
>>> p = ctypes.pointer(ctypes.c_uint32(2))
>>> for i in range(0, p):print(i)
... 
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'LP_c_uint' object cannot be interpreted as an integer

您需要取消引用指针以获取相应的值。

>>> d = p.contents.value
>>> d
2
>>> for i in range(0, d):print(i)
... 
0
1