2011-01-14 14 views
5

Quiero pasar una matriz de puntero a una función C.Cómo insertar un elemento en la matriz c_char_p

que se refieren a http://docs.python.org/library/ctypes.html#arrays

escribo el siguiente código.

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') 

y me aparece el siguiente error.

TypeError: '_ctypes.PyCArrayType' object does not support item assignment

¿Alguna idea de cómo puedo resolver esto? Quiero interactuar con

c_function(const char** array_of_string); 

Respuesta

12

Lo que hizo fue crear un tipo de matriz , no una matriz real, así que básicamente:

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

entonces usted puede hacer algo en la línea de:

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

... y proceda a llamar a su función C con la matriz names como parámetro.

Cuestiones relacionadas