开发者

ctypes pointer into the middle of a numpy array

开发者 https://www.devze.com 2023-03-15 15:37 出处:网络
I know how to get a ctypes pointer to the beginning of a numpy array: a = np.arange(10000, dtype=np.double)

I know how to get a ctypes pointer to the beginning of a numpy array:

a = np.arange(10000, dtype=np.double)
p = a.ctypes.data_as(开发者_StackOverflow中文版POINTER(c_double))
p.contents
c_double(0.0)

however, I need to pass the pointer to, let's say, element 100, without copying the array. There must be an easy way to do it but cannot find it.

Any hint appreciated.


Slicing a numpy array creates a view, not a copy:

>>> a = numpy.arange(10000, dtype=numpy.double)
>>> p = a[100:].ctypes.data_as(ctypes.POINTER(ctypes.c_double))
>>> p.contents
c_double(100.0)
>>> a[100] = 55
>>> p.contents
c_double(55.0)
0

精彩评论

暂无评论...
验证码 换一张
取 消