How to insert an element into the c_char_p array

I want to pass an array of char pointer to function C.

I turn to http://docs.python.org/library/ctypes.html#arrays

I am writing the following code.

from ctypes import * names = c_char_p * 4 # A 3 times for loop will be written here. # The last array will assign to a null pointer. # So that C function knows where is the end of the array. names[0] = c_char_p('hello') 

and I get the following error.

TypeError: '_ctypes.PyCArrayType' object does not support element Purpose

Any idea how I can solve this? I want to interact with

 c_function(const char** array_of_string); 
+6
python ctypes
source share
1 answer

What you did was create an array type, not an actual array, so basically:

 import ctypes array_type = ctypes.c_char_p * 4 names = array_type() 

Then you can do something line by line:

 names[0] = "foo" names[1] = "bar" 

... and go to calling your C function with an array of names as a parameter.

+13
source share

All Articles