Cannot convert char** to const char**

412 views
Skip to first unread message

Alexander Whatley

unread,
Jul 13, 2016, 1:55:41 AM7/13/16
to cython-users
I am trying to write a wrapper around a C++ library, and one of the functions I am trying to wrap in Cython has a variable of the form const char**. Apparently, it is impossible to initialize "const char**" from "char**" (see http://stackoverflow.com/questions/2463473/why-am-i-getting-an-error-converting-a-float-to-const-float), which presents a problem for me, as I am reading in an list of UTF-8 encoded strings from Python (call it x), which I then attempt to convert to char** using malloc and a for loop:

def f(x):
 cdef char** a = <char**> malloc(len(x) * sizeof(char*))
 for item, index in enumerate(x):
  a[index] = item
  ....

 What would the workaround here be? Is there an implementation of const_cast somewhere in libcpp? Thanks.

Alexander Whatley

unread,
Jul 15, 2016, 12:49:20 AM7/15/16
to cython-users
I solved this problem by rewriting line 2 as:

 cdef const char** a = <const char**> malloc(len(x) * sizeof(char*))
Reply all
Reply to author
Forward
0 new messages