Reputation: 103
Is there a way to get a pointer to an element in the middle of an ctypes array? Example:
lib = ctypes.cdll.LoadLibrary('./lib.so')
arr = (ctypes.c_int32 * 100)()
lib.foo(arr)
Now I don't want to call foo
with a pointer to the first element of arr
, but on the 10th. That would be equivalent to C notation &arr[9]
:
lib.foo(&arr[9])
Is there a smart way to do this?
Upvotes: 4
Views: 1273
Reputation: 177600
byref
has an optional parameter to add a byte offset to the address.
test.dll (Windows)
__declspec(dllexport) int foo(int* arr)
{
return *arr;
}
Example:
>>> from ctypes import *
>>> lib = CDLL('test')
>>> arr = (c_int * 100)(*range(100))
>>> arr[9]
9
>>> lib.foo(arr)
0
>>> lib.foo(byref(arr,sizeof(c_int) * 9))
9
Upvotes: 6