I'm trying to call to a C function that accept a pointer to pointer and redirect it to a internally-allocated 1D array, something along that line:
typedef myStruct {
const char* name;
int status;
} myStruct;
int foo(const myStruct** array, size_t* size);
I'm trying to wrap it with Python and NumPy, and get a NumPy array that wraps that internal memory (as read-only).
I've got the following:
arr = np.ctypeslib.ndpointer(dtype=np.dtype([('name', np.intp),
('status', np.int)]))()
size = ct.c_size_t()
_call('foo', ct.byref(arr), ct.byref(size))
arr = np.ctypeslib.as_array(arr, shape=(size.value,))
arr.flags.writeable = False # not our memory!
Where _call()
is a wrapper that check for the returned value.
I'm getting the following:
ValueError: '<P' is not a valid PEP 3118 buffer format string
over the call to as_array()
. What am I doing wrong?
EDIT: My goal here is to read the data as NumPy structured array, as I think it's the best way to describe a C array of structs in python.