在python ctypes中将字符串数组作为参数传递
这是一个后续行动。我有一个处理字符串数组的c函数。数据类型是静态的,因此这有助于:在python ctypes中将字符串数组作为参数传递,python,pointers,ctypes,Python,Pointers,Ctypes,这是一个后续行动。我有一个处理字符串数组的c函数。数据类型是静态的,因此这有助于: void cfunction(char strings[8][1024]) { printf("string0 = %s\nstring1 = %s\n",strings[0],strings[1]); strings[0][2] = 'd'; //this is just some dumb modification strings[1][2] = 'd'; return; } 我用python创建数
void cfunction(char strings[8][1024])
{
printf("string0 = %s\nstring1 = %s\n",strings[0],strings[1]);
strings[0][2] = 'd'; //this is just some dumb modification
strings[1][2] = 'd';
return;
}
我用python创建数据类型,并像这样使用它:
words = ((c_char * 8) * 1024)()
words[0].value = "foo"
words[1].value = "bar"
libhello.cfunction(words)
print words[0].value
print words[1].value
输出如下所示:
string0 = fod
string1 =
fod
bar
看起来我没有正确地将words对象传递给我的C函数;它不会//看到//第二个数组值,但写入内存中的位置不会导致segfault
声明的words对象还有一些奇怪的地方:
- 字[0]。值=foo
- len(字[0]。值)=3
- sizeof(字[0])=8
- repr(单词[0].raw)='foo\x00\x00\x00\x00\x00\x00'
为什么一个对象声明为1024个字符长,并给出截断的sizeof和原始值?我认为您需要将单词定义为:
words = ((c_char * 1024) * 8)()
这将是长度为1024的字符串的长度为8的数组。我认为您需要将单词定义为:
words = ((c_char * 1024) * 8)()
这将是长度为1024的字符串的长度为8的数组