Doo Dah
Doo Dah

Reputation: 4029

Python ctypes and char**

I have the following structures in C:

struct wordSynonym
{
    wchar_t* word;
    char** synonyms;
    int numSynonyms;
};

struct wordList
{
    wordSynonym* wordSynonyms;
    int numWords;
};

And, I have the following in Python:

class wordSynonym(Structure):
    _fields_ = [ ("word", c_wchar_p),
                  ("synonyms", POINTER(c_char_p)), # Is this correct?
                  ("numSynonyms", c_int) ];

class WordList(Structure):
    _fields_ = [ ("wordSynonyms", POINTER(wordSynonym)),
                 ("numWords", c_int)];

What is the correct way to reference char** in python? That is, in the Python code, is POINTER(c_char_p) correct?

Upvotes: 6

Views: 6504

Answers (1)

iabdalkader
iabdalkader

Reputation: 17312

I use this in my code:

POINTER(POINTER(c_char))

But I think both are equivalent.

Edit: Actually they are not http://docs.python.org/2/library/ctypes.html#ctypes.c_char_p

ctypes.c_char_p Represents the C char * datatype when it points to a zero-terminated string. For a general character pointer that may also point to binary data, POINTER(c_char) must be used. The constructor accepts an integer address, or a string.

So POINTER(POINTER(c_char)) is for binary data, and POINTER(c_char_p) is a pointer to a C null-terminated string.

Upvotes: 5

Related Questions