gpt4 book ai didi

Python ctypes 和 char**

转载 作者:太空狗 更新时间:2023-10-30 01:16:29 32 4
gpt4 key购买 nike

我在 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 空终止字符串的指针。

关于Python ctypes 和 char**,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13160957/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com