[Pyrex] newbie question about char array

Stefan Behnel behnel_ml at gkec.informatik.tu-darmstadt.de
Sun May 7 22:57:03 CEST 2006


Hi Mang,

Yuan Mang wrote:
> Thanks for your patient answer. I did read the tutorial but didn't find
> sections about how to cope with char ** . The c function takes a char**
> as variable and actually it needs rows of strings of the same length as
> input.
> So here I have a list of strings, [ string1, string2, .. stringN], how can I pass all the strings into the 
> c function?

char** only means: am array of char*. So, you can malloc one and the add the
pointers.

<untested>
cdef char** p
cdef char** p1
...
p = malloc(20 * sizeof(char*))
p1 = p
for string in string_list:
  p1[0] = string
  p1 = p1 + 1
...
some_C_call(p)
free(p)
</untested>

Note that you must keep references to all the Python strings during the call,
(which in this case means keeping the string_list referenced). The C pointers
are not enough.

Stefan



More information about the Pyrex mailing list