dpitch40
dpitch40

Reputation: 2691

Python-C API: Trouble packing C strings into a tuple

In the following code, I am trying to pack two null-terminated C strings (char pointers) into a Python tuple.

printf("word1 = '%s', word2 = '%s'\n", words1->wordArray[i], words2->wordArray[i]);
cmpArgs = Py_BuildValue("ss", words1->wordArray[i], words2->wordArray[i]);
printf("%s\n", PyString_AsString(PyTuple_GetItem(cmpArgs, 0)));

This produces output like:

word1 = '20', word2 = '20'
i┴

Why is the string different in the tuple than outside it? What am I doing wrong? Also, do I need to worry about incrementing and decrementing the reference count of this tuple? (I am creating it to pass to a Python function passed to C as a PyObject*)

Upvotes: 1

Views: 157

Answers (1)

dpitch40
dpitch40

Reputation: 2691

I think I got it--I was previously storing the words in a weird way using stack space. I started manually allocating space on the heap for them which seems to have fixed it.

Upvotes: 1

Related Questions