问题描述
我正在尝试使用ctypes将字符数组的数组传递给C函数.
I'm trying to pass an array of character arrays to a C function using ctypes.
void cfunction(char ** strings)
{
strings[1] = "bad"; //works not what I need.
strings[1][2] = 'd'; //this will segfault.
return;
}
char *input[] = {"foo","bar"};
cfunction(input);
由于我抛出的数组始终是静态定义的,我只是这样更改了函数声明和输入参数:
Since the array that I throw around is statically defined anyways,I just changed the function declaration and input parameter like so:
void cfunction(char strings[2][4])
{
//strings[1] = "bad"; //not what I need.
strings[1][2] = 'd'; //what I need and now it works.
return;
}
char input[2][4] = {"foo","bar"};
cfunction(input);
现在我遇到了如何定义这个多维字符的问题python中的数组.我曾以为会这样:
Now I run into the problem of how to define this multi-dimensional characterarray in python. I had thought it would go like so:
import os
from ctypes import *
libhello = cdll.LoadLibrary(os.getcwd() + '/libhello.so')
input = (c_char_p * 2)()
input[0] = create_string_buffer("foo")
input[1] = create_string_buffer("bar")
libhello.cfunction(input)
这给了我TypeError: incompatible types, c_char_Array_4 instance instead of c_char_p instance
.如果我将其更改为:
This gives me TypeError: incompatible types, c_char_Array_4 instance instead of c_char_p instance
. If I change it to:
for i in input:
i = create_string_buffer("foo")
然后我遇到了分割错误.同样,这似乎是构建2d数组的错误方法,因为如果我打印输入,我会看到None
:
Then I get segmentation faults. Also this looks like the wrong way to build the 2d array because if I print input I see None
:
print input[0]
print input[1]
# outputs None, None instead of "foo" and "foo"
我还遇到了使用#DEFINE MY_ARRAY_X 2
和#DEFINE MY_ARRAY_Y 4
将数组维保持在我的C文件中的问题,但是我不知道从libhello.so中获取这些常量的好方法,因此python在构造数据类型时可以引用它们.
I also run into the issue of using #DEFINE MY_ARRAY_X 2
and #DEFINE MY_ARRAY_Y 4
to keep the array dimensions straight in my C files, but don't know a good way to get these constants out of the libhello.so so that python can reference them when it constructs the datatypes.
推荐答案
使用类似
input = ((c_char * 4) * 2)()
input[0].value = "str"
input[0][0] == "s"
input[0][1] == "t" # and so on...
简单用法:
>>> a =((c_char * 4) * 2)()
>>> a
<__main__.c_char_Array_4_Array_2 object at 0x9348d1c>
>>> a[0]
<__main__.c_char_Array_4 object at 0x9348c8c>
>>> a[0].raw
'\x00\x00\x00\x00'
>>> a[0].value
''
>>> a[0].value = "str"
>>> a[0]
<__main__.c_char_Array_4 object at 0x9348c8c>
>>> a[0].value
'str'
>>> a[0].raw
'str\x00'
>>> a[1].value
''
>>> a[0][0]
's'
>>> a[0][0] = 'x'
>>> a[0].value
'xtr'
这篇关于python ctypes中的多维char数组(字符串数组)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!