ctypes.c_voidp is the ctypes type for a C void*, a generic pointer that can hold any memory address. It is an alias of ctypes.c_void_p, which is the name the Python documentation uses and the one you should write in new code.
You use it to pass opaque handles, buffers and user-data pointers between Python and C functions, and to receive such pointers as return values.
c_voidp is an alias of c_void_pvalue attributec_voidp is an alias of c_void_pIn the ctypes source, c_voidp is defined as another name for c_void_p and is kept for backward compatibility. Both refer to the same class, so they behave identically. Prefer c_void_p: it is the documented spelling and it reads like the C type.
The size is 8 bytes on 64-bit builds and 4 on 32-bit builds. The value attribute holds the address as a Python int, or None for a null pointer. Creating a c_void_p with a made-up address such as 1000 is fine for demonstration, but dereferencing it would crash the process.
value attributeA default c_void_p() is a null pointer, and bool() of it is False:
This makes it easy to check whether a C function returned NULL.
C APIs that hand out opaque handles usually return void*. Declare restype and argtypes so ctypes neither truncates the pointer nor guesses the wrong type. Without restype, ctypes assumes int and cuts a 64-bit pointer to 32 bits.
This example uses malloc and free from the C library on Linux or macOS:
When restype is c_void_p, ctypes returns the address as a plain Python int (or None for NULL), not a c_void_p instance. Pass that int straight back into functions whose argtypes are c_void_p. Memory you allocate with malloc is never freed by Python, so every malloc needs a matching free.
You can pass the address of a ctypes buffer by using ctypes.addressof() or by passing the buffer directly, since arrays convert to pointers automatically:
The pointer is only valid while buffer is alive. If the buffer is garbage collected, ptr dangles, so keep a reference to the buffer for as long as C code may use the address.
Many C libraries accept a callback plus a void* of user data. With ctypes you build the callback type with CFUNCTYPE and pass the user data as c_void_p:
Keep the callback object referenced in Python for as long as C can call it, or it is freed and the next call crashes.
If the C function takes a pointer to a known type, use ctypes.POINTER(type) rather than c_void_p. A typed pointer lets ctypes check the argument and lets you index it. Use c_void_p only when the C signature is truly void* or when the type is opaque. To convert between them use ctypes.cast(ptr, ctypes.POINTER(ctypes.c_int)).
| Need | Use |
|---|---|
Opaque handle or void* parameter | ctypes.c_void_p |
Pointer to a known type such as int* | ctypes.POINTER(ctypes.c_int) |
C string (char*) | ctypes.c_char_p |
| Output parameter | ctypes.byref(obj) |
ctypes.c_voidp a module?No. It is a class and an alias of ctypes.c_void_p. You import the ctypes module and use the class from it.
c_voidp and c_void_p?None in behaviour; c_voidp is an older alias for the same class. Use c_void_p in new code.
Read ptr.value, which is None for NULL, or use bool(ptr). When the function's restype is c_void_p, the returned value is already an int or None.
Common causes are a missing restype (the address was truncated to 32 bits), a buffer or callback that was garbage collected, and a wrong argtypes list. Set both argtypes and restype for every foreign function.
ctypes.c_void_p (also spelled c_voidp) represents C's void*. Use it for opaque handles, generic buffers and user-data pointers, declare argtypes and restype on every function, and keep Python objects alive for as long as C holds their addresses. Reach for POINTER(type) when the pointed-to type is known.