tags:

views:

66

answers:

2

Hello,

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

+2  A: 
def call_c(L):
    arr = (ctypes.c_char_p * len(L))()
    arr[:] = L
    lib.external_C(len(L), arr)
Aaron Gallagher
A: 

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.

Joakim