The ctypes.byref() function passes a ctypes object to a C function by reference, as a pointer to the object's memory. It is a function rather than a module, and it is the usual way to supply output parameters such as int * or struct * arguments.
byref() is cheaper than pointer() because it builds only a light wrapper for the call and does not create a full pointer object. The wrapper is meant for function arguments, and you cannot dereference it in Python.
Many C functions return values through pointer arguments. In Python you create a ctypes object to hold the result, pass byref(obj), and read obj.value or the structure fields afterwards. The signature is byref(obj, offset=0). The optional offset adds a number of bytes to the address, which is handy for writing into the middle of a buffer.
The C library function frexp(double x, int *exp) splits a number into a mantissa and an exponent, and returns the mantissa while storing the exponent through the pointer:
Because 48 = 0.75 * 2**6, the function returns 0.75 and writes 6 into exponent. Setting argtypes and restype is important: without them ctypes assumes int for everything and the double argument and return value would be wrong. This example uses a Unix-style math library name. On Windows, load a different library such as the C runtime or a DLL of your own.
The same pattern works for structures. The C function fills the structure in place:
Windows API functions such as GetClientRect follow exactly this pattern with a RECT structure. No copy of the structure is made, so the C code writes straight into your Python object.
byref(obj) | pointer(obj) | |
|---|---|---|
| Returns | Light argument wrapper | Full pointer instance |
| Speed | Faster | Slower, creates a new type if needed |
| Dereferencing in Python | Not possible | p.contents and p[0] |
| Store in a structure field | No | Yes |
| Typical use | Call arguments only | Data structures, linked nodes |
If you only need to hand an argument to a function, use byref(). If you must keep the pointer around, store it in a structure, or read through it from Python, use pointer().
byref(buf, offset) points offset bytes past the start of the object. This lets a C function fill a later part of a buffer without any slicing:
Keep offset within the object. ctypes does not check bounds, so an offset that passes the end lets C code write outside your buffer.
After the call, the ctypes object holds the value written by C. A simple type exposes it through .value, a structure through its fields, and an array through indexing:
Arrays decay to pointers automatically, so you pass numbers itself rather than byref(numbers).
byref(x) for a parameter declared as a plain value type. Match argtypes: POINTER(c_int) accepts byref(c_int()).int and expecting it to be modified. Python ints are immutable, so use c_int or another ctypes type.byref(Rect()) and then read the result from a temporary you cannot reach.No. It is a function in the ctypes module that returns a lightweight pointer wrapper for one function call.
Use byref() for call arguments. Use pointer() when you need to store the pointer or dereference it from Python.
No. The C function receives the address of your object, so it reads and modifies the original memory.
It is strongly recommended. With argtypes set, ctypes checks the argument type and converts values correctly, and you avoid crashes caused by wrong default assumptions.
ctypes.byref() passes an object's address to a C function without creating a full pointer, which makes it the standard way to handle output parameters. Declare argtypes and restype, keep the target object alive, and reach for pointer() only when you need to store or dereference the pointer in Python.