8

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?

1

1 Answer 1

8

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') >>> 
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you this helps very much. Turns out I was looking at python 2.7 documentation which is why I was so confused.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.