ctypes.POINTER is a function that creates a pointer type for a given ctypes type: ctypes.POINTER(ctypes.c_int) is the Python equivalent of C's int *. You use it to declare pointer parameters and return values of foreign functions, and to build structures that hold pointers.
It is not a module, despite how it is often searched. It lives in the ctypes module, and it works together with ctypes.pointer(), which creates a pointer instance, and ctypes.byref(), which passes one cheaply.
Three names look similar and do different jobs:
| Name | What it returns | Typical use |
|---|---|---|
POINTER(T) | A pointer type (a class) | In argtypes, restype and _fields_ |
pointer(obj) | A pointer instance that points to obj | When you need a real pointer object |
byref(obj) | A lightweight pass-by-reference argument | Out-parameters in a function call |
byref is faster than pointer because it does not build a full pointer object, but it can only be used as a call argument.
A pointer exposes the object it points to as .contents, and supports indexing like a C pointer. Reading .contents returns a new ctypes object that shares the same memory, so changing it changes the original.
Indexing performs no bounds checks. Writing p[10] on a four-element array corrupts memory just as it would in C, so keep the length alongside the pointer.
A pointer created without an argument is a NULL pointer. It is false in a boolean context, and reading .contents raises ValueError.
Always test with if p: before dereferencing a pointer returned by C code.
Declare pointer parameters with POINTER(T) in argtypes. This example calls the C library function qsort, whose comparison callback receives two int * pointers:
For an out-parameter, create the object and pass byref:
When a function returns a pointer, set restype = POINTER(T). The result is a pointer into memory owned by the C side, so find out from the library documentation who frees it.
Pointers in a structure are declared with POINTER in _fields_. A structure that points to itself, such as a linked list node, needs the field added after the class exists:
The head node does not own tail. If tail is garbage collected while head is still in use, head.next dangles. Keep a Python reference to every object a pointer targets.
ctypes.cast(obj, ctypes.POINTER(T)) reinterprets memory as another pointer type, which you use for void * data and for treating a buffer as an array of another type.
cast keeps a reference to the source object, so the memory stays alive while the new pointer exists.
POINTER(T) returns a type; pointer(obj) returns an instance that points at obj. Use the first for declarations and the second to create a value.
Use byref when you only need to pass an argument to a function, which is the common case for out-parameters. It is cheaper.
A NULL pointer is false, so if not p: detects it. Accessing .contents on a NULL pointer raises ValueError.
Use ctypes.addressof(obj) for an object, or ctypes.cast(p, ctypes.c_void_p).value for a pointer.
ctypes.POINTER(T) builds the pointer type that matches C's T *, pointer() creates pointer instances, and byref() passes them cheaply to functions. Check for NULL, avoid indexing past the end of buffers, and keep Python references to anything a pointer targets so the memory stays valid.