问题描述
有没有办法可以在 Cython 中将指针移动到 n 个字节之后?
cdef void *n_ary
cdef void *eightbytes
cdef void *n_ary_plus8
ary = cnp.PyArray_DATA(input_array)
eightbytes = 8
n_ary_plus8 = ary + eightbytes
最后一行出现错误 Invalid operand types for '+' (void *; void *)
,告诉我它不知道如何添加指针地址 ary 和指针八字节。看起来这应该是显而易见的,但我在手册或八月参考中找不到任何内容。
解决方法
void*
算术在 C 和 C++ 标准中是不允许的。
如果要这样做,则需要将指针转换为 char*
类型。
有关详细信息,请查看 this 和 this。使用此类指针转换(填充和对齐)要非常小心 strict aliasing rule。
,我能够做我需要做的事情
cdef void *n_ary
cdef int eightbytes
cdef void *n_ary_plus8
ary = cnp.PyArray_DATA(input_array)
eightbytes = 8
n_ary_plus8 = &ary[eightbytes]