Python ctypes和char **

前端之家收集整理的这篇文章主要介绍了Python ctypes和char **前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我在C中有以下结构:

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

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

而且,我在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)];

在python中引用char **的正确方法是什么?也就是说,在Python代码中,POINTER(c_char_p)是否正确?

最佳答案
我在我的代码中使用它:

POINTER(POINTER(c_char))

但我认为两者都是等价的.

编辑:
实际上他们不是
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.

所以POINTER(POINTER(c_char))用于二进制数据,POINTER(c_char_p)是指向C以null结尾的字符串的指针.

猜你在找的Python相关文章