加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

Python ctypes和char **

发布时间:2020-12-16 21:58:54 所属栏目:Python 来源:网络整理
导读:我在C中有以下结构: struct wordSynonym{ wchar_t* word; char** synonyms; int numSynonyms;};struct wordList{ wordSynonym* wordSynonyms; int numWords;}; 而且,我在Python中有以下内容: class wordSynonym(Structure): _fields_ = [ ("word",c_wchar_

我在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结尾的字符串的指针.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读