Best Division

Spread Your Knowledge

  • All topics
  • About us
  • Contact us
  • Privacy policy
  • Terms of service
  • Copyright
  • Facebook
  • Twitter
  • Instagram
  • Youtube
  • LinkedIn
  • Pinterest
  • Github
  • Tumblr

© 2026 Best Division

How to declare a variable of type c_char_p in ctypes?

Python
Jayant Kumar
Jayant Kumar@jayantkumar314
PublishedJul 15, 2023
UpdatedNov 17, 2026
5 min read

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.

Table of Contents

  • Declaring and reading a c_char_p
  • Changing the pointer, not the characters
  • Mutable buffers with create_string_buffer
  • Using c_char_p in function signatures
  • c_char_p, c_wchar_p and c_void_p
  • Frequently Asked Questions
    • Why does c_char_p("text") raise a TypeError?
    • How do I get a normal string out of a c_char_p?
    • When should I use create_string_buffer instead?
    • How do I create a NULL c_char_p?
  • Summary

Declaring and reading a c_char_p

python

.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:

python

Passing a str fails in Python 3:

python

Changing the pointer, not the characters

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.

python

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.

Mutable buffers with create_string_buffer

For functions that fill or edit a string, create a writable c_char array and pass that. It converts automatically where a char * is expected.

python

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.

TaskUse
Pass read-only text to Cc_char_p(b"text") or bytes directly
C writes into your memorycreate_string_buffer(n)
Receive a string from Crestype = c_char_p
Receive memory you must freerestype = c_void_p, then ctypes.string_at()

Using c_char_p in function signatures

Declare 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:

python

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_p

c_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.

Frequently Asked Questions

Why does 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").

How do I get a normal string out of a c_char_p?

Call .value.decode("utf-8"), after checking that .value is not None. Use the encoding that the C library actually produces.

When should I use 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.

How do I create a NULL c_char_p?

Call it with no argument or with None: ctypes.c_char_p(). Its .value is then None.

Summary

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.

Share