To declare a c_char_p variable, pass it a bytes object: s = ctypes.c_char_p(b"Hello"). The type represents a C char * that points to a NUL-terminated byte string, and its .value returns bytes, or None when the pointer is NULL. In Python 3 you cannot pass a str; encode it first, or the constructor raises a TypeError.
Because Python bytes are immutable, c_char_p is for read-only strings. When C code needs to write into a buffer, use ctypes.create_string_buffer() instead.
c_char_pcreate_string_bufferc_char_p in function signaturesc_char_p, c_wchar_p and c_void_pc_char_p.value gives you the bytes up to the terminating NUL. Decode them with the encoding the C library uses; UTF-8 is common, but some Windows APIs use the ANSI code page. If you start with text, encode it:
Passing a str fails in Python 3:
A c_char_p holds a pointer. Assigning to .value makes it point at a different bytes object; it does not change the characters in place.
ctypes keeps a reference to the bytes object so it stays alive while the c_char_p exists. Do not hand that pointer to C code that modifies the string: the memory belongs to an immutable Python object, and writing into it can corrupt the interpreter.
create_string_bufferFor functions that fill or edit a string, create a writable c_char array and pass that. It converts automatically where a char * is expected.
Make the buffer large enough before the call. ctypes cannot check that a C function stays inside it, so an undersized buffer is a buffer overflow.
| Task | Use |
|---|---|
| Pass read-only text to C | c_char_p(b"text") or bytes directly |
| C writes into your memory | create_string_buffer(n) |
| Receive a string from C | restype = c_char_p |
| Receive memory you must free | restype = c_void_p, then ctypes.string_at() |
c_char_p in function signaturesDeclare argtypes and restype so that ctypes converts arguments and results for you. A function declared with c_char_p accepts a bytes object directly, and returns bytes:
When restype is c_char_p, ctypes converts the result to a Python bytes object automatically and returns None for NULL. That conversion copies the data and loses the original pointer. If the C function allocates the string and expects you to free it, declare restype = ctypes.c_void_p, read the text with ctypes.string_at(address), then pass the address to the matching free function.
c_char_p, c_wchar_p and c_void_pc_wchar_p is the wide-string counterpart, taking and returning str for wchar_t *. c_void_p holds an untyped address as an integer, and you can convert with ctypes.cast(ptr, ctypes.c_char_p) when you need to read it as a string.
c_char_p("text") raise a TypeError?In Python 3 a str is Unicode text, while char * is raw bytes. Use b"text" or "text".encode("utf-8").
c_char_p?Call .value.decode("utf-8"), after checking that .value is not None. Use the encoding that the C library actually produces.
create_string_buffer instead?Use it whenever C code writes into the memory, for example strcpy, snprintf or any API that fills an output buffer. c_char_p points at immutable data.
c_char_p?Call it with no argument or with None: ctypes.c_char_p(). Its .value is then None.
Declare a c_char_p with a bytes value, read it with .value, and decode it when you need text. It is a pointer to immutable data, so use create_string_buffer() when C must modify a string. Set argtypes and restype on the functions you call, and use c_void_p with string_at() when you have to free memory that C allocated.