5

I have the following Cython code (the syntax is based on the post 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)

It throws the compile-time error:

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'

For some reason an array of char instead of char* is getting generated. Therefore, can anyone help me to obtain an array of char* in Cython?

1 Answer 1

5

I have figured it out. The correct syntax is:

cdef char **array = <char**>malloc(n * sizeof(char*))
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.