如何将项目插入c_char_p数组 [英] How to insert item into c_char_p array

查看:98
本文介绍了如何将项目插入c_char_p数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将字符指针数组传递给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'
对象不支持项目
分配

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

有什么办法可以解决吗?我想与

Any idea how I can resolve this? I want to interface with

c_function(const char** array_of_string);


推荐答案

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

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

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

You can then do something along the lines of:

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

...并继续使用 names 数组调用C函数,如下所示:参数。

...and proceed to call your C function with the names array as parameter.

这篇关于如何将项目插入c_char_p数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆