访问ctypes联合中的数组

时间:2017-06-22 17:34:58

标签: python ctypes

我第一次使用python ctypes。我的目的是将python列表转换为位于ctype联合内部的数组。但是,当我尝试这样做时,我得到TypeError。我已粘贴下面的代码,因为我无法找到适用于此场景的任何合适示例: 请评论。

from ctypes import * 
class state_struct(Structure):
    _fields_ = [
                ("loc0", c_uint32 * 3),
                ("loc1", c_uint32 ),
                ("loc2", c_uint32 * 12),
                ("loc3", c_uint32 * 28),
                ("loc4", c_uint32 * 2),
               ]

class state_union(Union):
    _fields_ = [("state_struct", state_struct),
                ("data", c_uint32 * 46),

                ]
def populate_union(): 
    u = state_union()
    l = [1,2,3] 
    for i in iter(l):
        u.state_struct.loc0 = i 


populate_union()

TypeError:期望c_uint_Array_3实例,得到int

1 个答案:

答案 0 :(得分:1)

<Router> <div> <Header /> <main> <Route exact path="/" component={HomePage} /> <Route path="/cards" component={Example} /> </main> <Footer /> </div> </Router> 是一个长度为3的数组。如果要为它们指定整数,则需要索引其元素。

例如:

state_struct.loc0

或者您可以使用切片索引一次性分配整个列表:

l = [1,2,3]
for index, value in enumerate(l):
    u.state_struct.loc0[index] = value
相关问题