概述
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字符串?所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复