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。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值