How to return a union from a function in ctypes?
Declare a ctypes.Union that mirrors the C union, set restype, and read only the member the C code stored. Includes a tagged union pattern.
Spread Your Knowledge. Questions with answers, tutorials and articles, newest first.
Declare a ctypes.Union that mirrors the C union, set restype, and read only the member the C code stored. Includes a tagged union pattern.
Subclass ctypes.Structure, set it as the restype, and read C struct fields in Python, including pointer returns.
Define a ctypes.Union that matches the C union, then pass it by pointer or by value. Covers member overlap, tagged unions with _anonymous_ and ABI caveats.
Define a ctypes.Structure that mirrors the C struct, set argtypes, then pass it by value or by pointer. Includes returning structs, nesting and packing.
Build a ctypes array, declare argtypes with POINTER, and pass it with its length so the C function can read and modify the Python data.
Use POINTER in argtypes and byref or pointer at the call site. Examples for integers, structures, arrays and out parameters, plus the mistakes to avoid.
A ctypes Structure stores fields one after another, while a Union overlays them in the same memory. See sizes, packing and examples.
Learn the ctypes workflow for C libraries with strings, buffers, structs and errcheck error handling, using a full compilable example.
Load a shared library with ctypes.CDLL, declare argtypes and restype, then call C functions with ints, doubles, pointers and arrays.
Learn how to define a function prototype in Python ctypes with argtypes and restype, including pointers, arrays, structures and a type table.
Subclass ctypes.Union and list _fields_ so all members share memory. Covers type punning, tagged unions with _anonymous_ and byte order variants.
Subclass ctypes.Structure and list the fields in _fields_. Covers layout and padding, nesting, bit fields, pointers, packing and passing structures to C.
ctypes.Union describes a C union whose fields share the same memory. See tagged unions, anonymous members and how to read raw bytes safely in Python.
ctypes.Structure maps a C struct to a Python class using _fields_. See nested fields, arrays, pointers, packing, byte order and how to pass structs to C.
ctypes.POINTER(ctypes.py_object) is the type of PyObject **. It is for slots and arrays of Python objects used with CPython C API code.
POINTER(c_char_p) models a C char ** in ctypes. See strtol endptr, argv-style arrays of bytes, NULL handling and the memory ownership pitfalls to avoid.
ctypes.POINTER(ctypes.c_double) is the C type double *. It lets you pass double arrays and output parameters between Python and C libraries.
POINTER(c_int) models a C int * in ctypes. See output parameters with byref, integer arrays with qsort, NULL pointer checks and the pointer versus POINTER difference.
There is no ctypes.py_object_p. The real type is POINTER(py_object), named LP_py_object. See how to create it and use it safely.
py_object_Array_N is what ctypes creates for py_object times N: a fixed-size array of Python object references. Learn how it works.