Cython

时间:2015-11-10 10:00:11

标签: arrays cython

我有以下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数组吗?

1 个答案:

答案 0 :(得分:2)

我已经弄清楚了。正确的语法是:

cdef char **array = <char**>malloc(n * sizeof(char*))