Python ctypes和char **

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

我在C中有以下结构:

  1. struct wordSynonym
  2. {
  3. wchar_t* word;
  4. char** synonyms;
  5. int numSynonyms;
  6. };
  7. struct wordList
  8. {
  9. wordSynonym* wordSynonyms;
  10. int numWords;
  11. };

而且,我在Python中有以下内容

  1. class wordSynonym(Structure):
  2. _fields_ = [ ("word",c_wchar_p),("synonyms",POINTER(c_char_p)),# Is this correct?
  3. ("numSynonyms",c_int) ];
  4. class WordList(Structure):
  5. _fields_ = [ ("wordSynonyms",POINTER(wordSynonym)),("numWords",c_int)];

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

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

  1. 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相关文章