Passing array of strings as parameter in python ctypes
This is a followup to Multi-dimensional char array (array of strings) in python ctypes . I have a c function that开发者_运维百科 manipulates an array of strings. The data type is static, so this helps:
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;
}
I create the data type in python and use it like so:
words = ((c_char * 8) * 1024)()
words[0].value = "foo"
words[1].value = "bar"
libhello.cfunction(words)
print words[0].value
print words[1].value
The output looks like this:
string0 = fod
string1 =
fod
bar
It looks like I am improperly passing the words object to my C function; it doesn't //see// the second array value, yet writing to the location in memory doesn't cause a segfault.
Something else odd about the declared words object:
- words[0].value = foo
- len(words[0].value) = 3
- sizeof(words[0]) = 8
- repr(words[0].raw) = 'foo\x00\x00\x00\x00\x00'
Why is an object declared as 1024 characters long giving truncated sizeof and raw values?
I think you need to define words as:
words = ((c_char * 1024) * 8)()
This would be an array of length 8 of character strings of length 1024.
精彩评论