Assigning a new value to instances of the pointer types c_char_p, c_wchar_p, and c_void_p changes the memory location they point to, not the contents of the memory block (of course not, because Python strings are immutable):>>> s = "Hello, World"
>>> c_s = c_char_p(s)
>>> print c_s
c_char_p('Hello, World')
>>> c_s.value = "Hi, there"
>>> print c_s
c_char_p('Hi, there')
>>> print s # first string is unchanged
Hello, World
>>>You should be careful, however, not to
pass them to functions expecting
pointers to mutable memory. If you
need mutable memory blocks, ctypes has
a create_string_buffer function which
creates these in various ways. The
current memory block contents can be
accessed (or changed) with the raw
property, if you want to access it as
NUL terminated string, use the string
property:
ctypes教程说。我从中得到的结论是,只有当函数使用const char*时,传入python字符串才是有效的。记住,它不会有空终止。
我建议无论如何使用create_string_buffer。
最后
以上就是勤恳墨镜最近收集整理的关于pythonctype_是否可以通过ctype引用传递python字符串?的全部内容,更多相关pythonctype_是否可以通过ctype引用传递python字符串内容请搜索靠谱客的其他文章。
发表评论 取消回复