ctypes.POINTER(type) returns a new pointer type for the C type you pass in, so ctypes.POINTER(ctypes.c_int) is the Python equivalent of int *. You use it to declare argtypes and restype for C functions that take or return pointers, to build structures that point to other structures, and to read C arrays. To get a pointer to an existing ctypes object, call ctypes.pointer(obj) or, for function arguments, ctypes.byref(obj).
The difference between those three names trips up most people: POINTER makes a type, pointer makes an instance of that type, and byref makes a lightweight argument wrapper that is only valid in a call.
| Name | What it returns | Typical use |
|---|---|---|
ctypes.POINTER(ctypes.c_int) | A pointer type (class LP_c_int) | argtypes, restype, structure fields |
ctypes.pointer(obj) | A pointer instance that points to obj | Storing a pointer, reading .contents |
ctypes.byref(obj) | A light reference, not a full pointer object | Passing an out parameter in one call |
byref is faster than pointer because it does not allocate a pointer object, but it can only be used as a function argument. Use pointer when you need to keep the pointer, index it or pass it around.
A pointer instance behaves like a C pointer with Python syntax. Read and write the target through .contents, or index it like an array.
Note that p.contents gives you the original ctypes object (a view of the same memory), so assigning to its .value changes n. Assigning a new object to p.contents instead re-points the pointer.
Calling a pointer type with no arguments creates a NULL pointer. A NULL pointer is falsy, and reading .contents on it raises ValueError.
Always test a pointer returned from C with if ptr: before dereferencing it. Dereferencing a NULL pointer through indexing (null[0]) is not checked and can crash the interpreter.
Declare the pointer parameter in argtypes; without it ctypes converts Python integers to int, which truncates 64-bit pointers. The example below sorts a C array with qsort from the C library, using a callback whose parameters are POINTER(c_int). It runs on Linux and macOS; on Windows use msvcrt instead.
ctypes converts the array values to a POINTER(c_int) automatically because an array of c_int is compatible with that pointer type. Inside the callback, a[0] dereferences the pointer. Keep a reference to the CompareFunc(compare) object for as long as C may call it; here it lives for the duration of the call.
When C writes a result through a pointer argument, create a ctypes variable and pass byref(variable).
A structure that contains a pointer to its own type needs the class to exist before the field is declared. Create the class first and assign _fields_ afterwards.
The loop ends because the last node's next is a NULL pointer, which is falsy. The Python variables second and third must stay alive while the list is used, since pointers do not own the memory they point to.
ctypes.cast(obj, POINTER(type)) reinterprets a pointer or array as another pointer type, like a C cast.
Casting does not check bounds. Indexing past the end of the buffer reads whatever is in memory there, so keep track of the length yourself.
func(ctypes.POINTER(ctypes.c_int)). Pass byref(x) or pointer(x) instead.argtypes, which makes ctypes guess and can truncate addresses.pointer(x) in a hot loop when byref(x) would do.POINTER takes a type and returns a pointer type (a class). pointer takes an object and returns an instance of that pointer type that points at the object.
Call the pointer type with no arguments, for example ctypes.POINTER(ctypes.c_int)(). You can also pass None where a pointer is expected, and test a pointer for NULL with if not ptr:.
Index the pointer or slice it with a known length, for example ptr[:count], which returns a Python list. ctypes cannot know the length, so you must get it from the C API.
Yes, a pointer created with pointer(obj) holds a Python reference to obj. A pointer you receive from C does not own anything, so the C side decides how long the memory is valid.
ctypes.POINTER(t) creates the pointer type used for argtypes, restype and structure fields. ctypes.pointer(obj) creates an instance, byref(obj) passes an argument cheaply, and .contents or indexing dereferences. Check for NULL with a truthiness test, set argtypes on every function, and keep the memory you point to alive.