我有以下c函数:
extern "C" {
void f( int* array, int arraylen ) {
for(int i = 0; i < arraylen; i++) {
array[i] = g() // mutate the value array[i];
}
}
}
python中的以下代码:
import ctypes
plib = ctypes.cdll.LoadLibrary('./mylib.so')
_f = plib.f
_f.restype = None
_f.argtypes = [ ctypes.POINTER(ctypes.c_int), ctypes.c_int ]
seqlen = 50
buffer = ctypes.c_int * seqlen
_f( buffer, seqlen )
但是,此代码段会随着以下回溯而终止:
Traceback (most recent call last):
File "particle.py", line 9, in <module>
_f( buffer, seqlen )
ctypes.ArgumentError: argument 1: <type 'exceptions.TypeError'>: expected LP_c_int instance instead of _ctypes.ArrayType