我有以下Cython
代码(语法基于帖子https://stackoverflow.com/a/13983740/4189299):
from libc.stdlib cimport malloc, free
def test_func():
cdef int n = 10
cdef char *array = <char *>malloc(n * sizeof(char*))
for i in range(n):
array[i] = NULL
free(array)
抛出编译时错误:
Error compiling Cython file:
------------------------------------------------------------
...
def test_func():
cdef int n = 10
cdef char *array = <char *>malloc(n * sizeof(char*))
for i in range(n):
array[i] = NULL
^
------------------------------------------------------------
test.pyx:7:19: Cannot assign type 'void *' to 'char'
出于某种原因,生成了char
而不是char*
的数组。因此,任何人都可以帮助我在char*
中获得Cython
数组吗?
答案 0 :(得分:2)
我已经弄清楚了。正确的语法是:
cdef char **array = <char**>malloc(n * sizeof(char*))