繁体   English   中英

在python ctypes中将字符串数组作为参数传递

[英]Passing array of strings as parameter in python ctypes

这是python ctypes中的多维char数组(字符串数组)的后续版本。 我有一个可以处理字符串数组的ac函数。 数据类型是静态的,因此这有助于:

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

看来我没有正确地将单词object传递给我的C函数; 它没有//第二个数组的值,但是写入内存中的位置不会引起段错误。

声明的单词对象还有其他奇怪之处:

  • 单词[0] .value = foo
  • len(words [0] .value)= 3
  • sizeof(words [0])= 8
  • repr(words [0] .raw)='foo \\ x00 \\ x00 \\ x00 \\ x00 \\ x00'

为什么将一个声明为1024个字符的对象声明为sizeof和原始值都截短了?

我认为您需要将单词定义为:

words = ((c_char * 1024) * 8)()

这将是长度为1024的字符串的长度为8的数组。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM