To pass a string to a C function that takes char **, build an array of ctypes.c_char_p from encoded bytes, declare the parameter as ctypes.POINTER(ctypes.c_char_p), and pass the array. A single string becomes an array of length one, and a NULL-terminated list gets one extra None slot.
Python str objects are Unicode, so you must encode them first. c_char_p points at bytes, and ctypes does not convert a str for a char * argument in Python 3.
A char ** is a pointer to a pointer to char. It appears in three common shapes, and the right Python code depends on which one you have:
| Shape | Example | Python side |
|---|---|---|
| Array of strings (input) | argv-style list | Array of c_char_p, often with a None terminator |
| Output string | int get_name(char **out) | byref(c_char_p()) and read .value |
| In-out pointer | parser cursor | c_char_p object passed with byref() |
Check the library documentation to find out who owns the memory and whether a count or a terminator marks the end.
Consider this C function:
Build it with gcc -shared -fPIC -o libexample.so example.c. Then call it from Python:
ctypes.c_char_p * 3 creates an array type holding three char * values. Calling it with the byte strings fills it, and ctypes passes it as char **. The bytes objects must stay alive during the call. Here the array holds references to them, so that is safe.
To pass one string, use a list with a single element, ArrayType = ctypes.c_char_p * 1 and a count of 1.
Many C functions expect the list to end with NULL instead of taking a count, as with execvp or argv. Allocate one more slot than there are strings and leave it None:
The last element is None, which ctypes writes as a NULL pointer. Without the terminator the C code reads past the end of the array.
When the C side allocates a string and returns it through char **out, pass byref() of a c_char_p:
Read the text with out.value, which copies the bytes into a Python bytes object. Free the C allocation with the library's own function. Python does not free memory that C allocated with malloc.
c_char_p pointing at Python bytes must be treated as read-only. If C writes into the string, use a writable buffer instead:
Pick the encoding the C library expects. UTF-8 is the usual choice on Linux and macOS. Some Windows APIs use the ANSI code page, so check the documentation. Use os.fsencode() for file system paths. A bytes value containing an embedded zero byte is cut short by C, so do not use c_char_p for binary data. Use POINTER(c_ubyte) or c_void_p with a length for that.
c_char_p accepts bytes or None, not str. Encode with text.encode("utf-8") before the call.
Create an array of length one with (ctypes.c_char_p * 1)(b"text") and pass it. If the API expects a NULL terminator, make the array two slots long and leave the last as None.
Yes, for as long as C may read them. An array of c_char_p created from bytes keeps references to them, so keep the array itself in scope as well.
No. c_wchar_p maps to wchar_t * for wide strings. Use it when the C signature says wchar_t, not char.
For a char ** parameter, encode each string to bytes, build an array with ctypes.c_char_p * n, and declare ctypes.POINTER(ctypes.c_char_p) in argtypes. Add a trailing None for NULL-terminated lists, use byref(c_char_p()) for output strings, and release memory with the library's free function. Writable buffers come from create_string_buffer().