Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python ctypes指针问题_Python_Object_Pointers_Declaration_Ctypes - Fatal编程技术网

Python ctypes指针问题

Python ctypes指针问题,python,object,pointers,declaration,ctypes,Python,Object,Pointers,Declaration,Ctypes,我在阅读ctypes教程时,遇到了以下问题: s = "Hello, World" c_s = c_char_p(s) print c_s c_s.value = "Hi, there" 但我一直在使用这样的指针: s = "Hello, World!" c_s = c_char_p() c_s = s print c_s c_s.value Traceback (most recent call last): File "<pyshell#17>", line 1, in

我在阅读ctypes教程时,遇到了以下问题:

s = "Hello, World"
c_s = c_char_p(s)
print c_s
c_s.value = "Hi, there"
但我一直在使用这样的指针:

s = "Hello, World!"
c_s = c_char_p()
c_s = s
print c_s
c_s.value

Traceback (most recent call last):
  File "<pyshell#17>", line 1, in <module>
    c_s.value
AttributeError: 'str' object has no attribute 'value'
s=“你好,世界!”
c_s=c_char_p()
c_s=s
打印c_
c_.值
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
c_.值
AttributeError:“str”对象没有属性“value”
为什么当我以一种方式做时,我可以访问c_.value,而当我以另一种方式做时,没有值对象


谢谢大家

在第二个示例中,您得到了以下语句:

c_s = c_char_p()
c_s = s
ctypes
模块无法中断,在上述情况下,第二个赋值将
c\u s
名称从刚刚创建的
c\u字符p
对象重新绑定到
s
对象。实际上,这会丢弃新创建的
c\u char\p
对象,并且您的代码会在问题中产生错误,因为常规Python字符串没有
.value
属性

请尝试:

c_s = c_char_p()
c_s.value = s

看看这是否符合你的期望。

Ohh。哇,这是一个很好的基本答案。谢谢