1

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?

1 Answer 1

5

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.