如何将项插入c_char_p数组

2024-09-07 12:38:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我想把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')

我得到以下错误。在

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

你知道我怎么解决这个问题吗?我想与

^{pr2}$

Tags: the函数orghttpdocsnameshtmllibrary
1条回答
网友
1楼 · 发布于 2024-09-07 12:38:16

您所做的是创建一个数组类型,而不是实际的数组,因此基本上:

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

然后,您可以按照以下思路进行操作:

^{pr2}$

…并继续使用names数组作为参数调用C函数。在

相关问题 更多 >