I'd like to do this to pass a string to a Cython code:
# test.py
s = "Bonjour"
myfunc(s)
# test.pyx
def myfunc(char *mystr):
cdef int i
for i in range(len(mystr)): # error! len(mystr) is not the length of string
print mystr[i] # but the length of the *pointer*, ie useless!
but as shown in comment, here it doesn't work as expected.
The only workaround I've found is passing also the length as parameter of myfunc. Is it correct? Is it really the simplest way to pass a string to a Cython code?
# test.py
s = "Bonjour"
myfunc(s, len(s))
# test.pyx
def myfunc(char *mystr, int length):
cdef int i
for i in range(length):
print mystr[i]
str.str@user2357112 ? Would it really bestrorc_stringor something else?char *mystrin C is just a pointer to some array (of characters or numbers), there is no way to know how large it is unless you explicitly provide its size withint lengthas you have done. If you want to avoid this, usedef myfunc( str mystr):as @ user2357112 has indicated.