Define the FP struct and pass an instance by reference via ctypes.byref(instance). The FP record can be implemented in various ways. Since ctypes arrays are bounds checked, I prefer to use a property to handle a case like FP.array. For example:
class FP(ctypes.Structure):
_fields_ = (('rows', ctypes.c_ushort),
('cols', ctypes.c_ushort),
('_array', ctypes.c_double * 1))
def __init__(
self, rows, cols, array=None, *,
USHRT_MAX=((1 << (ctypes.sizeof(ctypes.c_ushort) * 8)) - 1),
):
# ctypes.c_ushort requires int type
self.rows = rows
self.cols = cols
if rows < 1 or cols < 1 or rows > USHRT_MAX or cols > USHRT_MAX:
raise ValueError('rows and cols must be greater than 0 and '
'less than {}'.format(USHRT_MAX + 1))
length = rows * cols
if length > 1:
ctypes.resize(self, ctypes.sizeof(self) +
ctypes.sizeof(ctypes.c_double) * (length - 1))
if array is not None:
self.array[:] = array
@property
def array(self):
length = self.rows * self.cols
offset = __class__._array.offset
return (ctypes.c_double * length).from_buffer(self, offset)