I have the following line in python:
a = (ctypes.c_float*10)()
Here a is initialized as an object with _repr_ result:
<__main__.c_float_Array_10 object at 0x7fbb3e6cf340>
My understanding is that this object is some sort of a pointer that can interoperate between python and C functions. How does this work and how are we able to multiply ctypes.c_float
class with and integer?
c_float
is a type corresponding to C float
.
c_float * 10
is an array of that type corresponding to C float[10]
.
(c_float * 10)()
is an instance of the 10-element float array.
A DLL with a C function of float sum(float* arr, size_t size);
could be called with:
import ctypes as ct
dll = ct.CDLL('./somelibrary.dll')
# Inform ctypes of the type of arguments and return value.
dll.sum.argtypes = ct.POINTER(ct.c_float), ct.c_size_t
dll.sum.restype = ct.c_float
# make an instance of an array of 10 float values.
a = (ct.c_float * 10)(1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8, 9.9, 10.0)
result = dll.sum(a, len(a))