Using ctypes methods in python gives an unexpected error

I am new to python and ctypes. I am trying to complete a seemingly easy task, but getting unexpected results. I am trying to pass a string to a c function, so I am using type c_char_p, but this gives me an error message. Simple, this is what happens:

>>>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?

+8
python ctypes
source share
1 answer

In Python 3.x, "text literal" indeed a unicode object. You want to use a byte string literal, for example 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') >>> 
+8
source share

All Articles