I have a C function which expects a list \0 terminated strings as input:
void external_C( int length , const char ** string_list) {
// Inspect the content of string_list - but not modify it.
}
From python (with ctypes) I would like to call this function based on a list of python strings:
def call_c( string_list ):
lib.external_C( ?? )
call_c( ["String1" , "String2" , "The last string"])
Any tips on how to build up the datastructure on the python side? Observe that I guarantee that the C function will NOT alter content of the strings in string_list.
Regards
joakim
def call_c(L): arr = (ctypes.c_char_p * len(L))() arr[:] = L lib.external_C(len(L), arr)
Thank you very much; that worked like charm. I also did an alternative variation like this:
def call_c( L ): arr = (ctypes.c_char_p * (len(L) + 1))() arr[:-1] = L arr[ len(L) ] = None lib.external_C( arr )
And then in C-function I iterated through the (char **) list until I found a NULL.
expiries = ["1M", "2M", "3M", "6M","9M", "1Y", "2Y", "3Y","4Y", "5Y", "6Y", "7Y","8Y", "9Y", "10Y", "11Y","12Y", "15Y", "20Y", "25Y", "30Y"]
expiries_bytes = []
for i in range(len(expiries)):
expiries_bytes.append(bytes(expiries[i], 'utf-8'))
expiries_array = (ctypes.c_char_p * (len(expiries_bytes)+1))()
expiries_array[:-1] = expiries_bytes
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With