What's the difference between LP_* pointers and *_p pointers in ctypes? (and weird interaction with structs)

前端 未结 1 615
再見小時候
再見小時候 2021-01-05 01:03

I\'m having trouble understanding the difference between LP_* (e.g. LP_c_char) and *_p (e.g. c_char_p) pointers in Python ctypes. Is there documentation distinguishing them?

相关标签:
1条回答
  • 2021-01-05 01:13

    http://docs.python.org/library/ctypes.html#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.

    c_char_p is mapped to Python's str type because it's assuming you're referring to a string, which is generally the case when you're using char * in C. LP_c_char makes no such assumption.

    Fundamental data types, when returned as foreign function call results, or, for example, by retrieving structure field members or array items, are transparently converted to native Python types. In other words, if a foreign function has a restype of c_char_p, you will always receive a Python string, not a c_char_p instance.

    0 讨论(0)
提交回复
热议问题