简体   繁体   English

如何使用Python和ctypes处理指向指针的指针

[英]How to handle a pointer to a pointer with Python and ctypes

I'm trying to model a struct found in a library I'm wrapping, where the struct has a pointer-to-a-pointer like this: 我正在尝试对在我包装的库中找到的结构进行建模,该结构中的指针指向如下所示:

typedef struct item_t {
  char* name;
}

typedef struct container_t {
  item_t **items;
}

How, when modeling with Python's ctypes module's Structure class, would I represent an array of pointers with a variable length? 在使用Python的ctypes模块的Structure类进行建模时,如何表示一个长度可变的指针数组?

You can use POINTER and convert item_t ** to POINTER(POINTER(item_t)) : 您可以使用POINTER并将item_t **转换为POINTER(POINTER(item_t))

from ctypes import *

class item_t(Structure):
    _fields_ = [
        ('name', c_char_p),
    ]


class container_t(Structure):
    _fields_ = [
        ('items', POINTER(POINTER(item_t))),
    ]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM