Carson
Carson

Reputation: 17771

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?

Upvotes: 1

Views: 132

Answers (1)

user1129665
user1129665

Reputation:

You can use POINTER and convert item_t ** to 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))),
    ]

Upvotes: 1

Related Questions