如何将项目插入c_char_p数组

9
我希望将 char指针数组 传递给C函数。
我参考了http://docs.python.org/library/ctypes.html#arrays 我编写了以下代码。
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')

我遇到了以下错误:

类型错误:'_ctypes.PyCArrayType' 对象不支持项目赋值

你有什么想法可以解决这个问题吗?我想要与某些东西进行接口交互。

c_function(const char** array_of_string);
1个回答

17
你所做的是创建了一个数组类型,而不是实际的数组,基本上说:

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

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

你可以按照以下方式进行操作:

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

然后使用names数组作为参数调用您的C函数。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接