我是靠谱客的博主 勤恳墨镜,最近开发中收集的这篇文章主要介绍pythonctype_是否可以通过ctype引用传递python字符串?,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

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字符串?所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(69)

评论列表共有 0 条评论

立即
投稿
返回
顶部