I'm pretty new to python and ctypes. I'm trying to accomplish a seemingly easy task but am getting unexpected results. I'm trying to pass a string to a c function so I'm using the c_char_p type but it's giving me an error message. To simply it, this is whats happening:
>>>from ctypes import *
>>>c_char_p("hello world")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: string or integer address expected instead of str instance
What's going on here?
In Python 3.x, the "text literal"
is really a unicode object. You want to use the byte-string literal like b"byte-string literal"
>>> from ctypes import *
>>> c_char_p('hello world')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: string or integer address expected instead of str instance
>>> c_char_p(b'hello world')
c_char_p(b'hello world')
>>>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With