Python 用数组定义结构
我通常使用numpy数组,因此在Python中定义了以下Python 用数组定义结构,python,numpy,struct,ctype,Python,Numpy,Struct,Ctype,我通常使用numpy数组,因此在Python中定义了以下ctypestype arr_double = numpy.ctypeslib.ndpointer(dtype=np.double, flags='C_CONTIGUOUS') 然后我在C中有以下结构 struct mystruct { double *array; size_t size; }; 我尝试在Python中使用ctypes定义这个结构,如下所示 class MyStruct(ctypes.Structure
ctypes
type
arr_double = numpy.ctypeslib.ndpointer(dtype=np.double, flags='C_CONTIGUOUS')
然后我在C中有以下结构
struct mystruct {
double *array;
size_t size;
};
我尝试在Python中使用ctypes定义这个结构,如下所示
class MyStruct(ctypes.Structure):
_fields_ = [("array", arr_double), ("size", ctypes.c_size_t)]
然而,这给了我一个错误
second item in _fields_ tuple (index 2) must be a C type
在ctypes中定义此结构的正确方法是什么?有没有办法使用numpy的ctypes类型?如果不是,我将如何将numpy数组转换为“C类型”