What is ctypes.ARRAY in Python and how do you use it?
ctypes.ARRAY builds fixed-size arrays of C values. Learn the preferred c_int * 5 syntax, initialisation, indexing and passing arrays to C code.
Spread Your Knowledge. Questions with answers, tutorials and articles, newest first.
ctypes.ARRAY builds fixed-size arrays of C values. Learn the preferred c_int * 5 syntax, initialisation, indexing and passing arrays to C code.
c_float_Array is the type Python builds when you write c_float * n: a fixed-size C array of 32-bit floats you can pass to C libraries as a float pointer.
A ctypes double array, created as c_double * N and named c_double_Array_N, is a fixed-size block of 64-bit floats you can fill, slice and pass to C code.
A ctypes c_longlong array is a fixed-size block of signed 64-bit integers made with c_longlong * n. See indexing, C calls and NumPy sharing.
A ctypes c_ulong array is a fixed-length C array of unsigned long values, created with ctypes.c_ulong * N and passed to C functions as a pointer.
Multiplying ctypes.c_void_p by N creates a fixed-size array of void pointers. See how to fill it, keep objects alive and pass it to C as void **.
Multiplying ctypes.c_wchar by N gives a fixed-size wchar_t buffer. See how to create, fill and pass it to C, and why the character width varies.
A ctypes character array, built as c_char * N and named c_char_Array_N, is a fixed-size mutable buffer for C strings, read through .value and .raw.
ctypes.c_buffer is not a real type but a wrapper around create_string_buffer. See how to build mutable byte buffers and pass them safely to C code.
ctypes.PYFUNCTYPE builds prototypes for functions called with the GIL held. See how it differs from CFUNCTYPE and how to use pythonapi.
ctypes.WINFUNCTYPE builds stdcall function prototypes on Windows. See an EnumWindows callback, lifetime rules and how it differs from CFUNCTYPE.
CFuncPtr is the base class behind every ctypes function pointer. See how CFUNCTYPE creates prototypes, how to write callbacks, and how to keep them alive.
ctypes.PyCFuncPtr is the private base class behind every ctypes function pointer. See how to create, call and safely use function pointers.
OleDLL is a Windows-only ctypes loader that treats returns as HRESULT and raises OSError on failure. Compare it with WinDLL and see working ole32 examples.
ctypes.WinDLL loads a Windows DLL and exposes its functions using the stdcall convention. See argtypes, error handling and the difference from CDLL.
ctypes.cast converts a pointer to another ctypes pointer type without copying data. See byte views, void pointers, structure casts and the pitfalls.
ctypes.wstring_at(address, size=-1) copies a wchar_t string from a raw memory address into a Python str, with examples and safety rules.
ctypes.string_at(address, size=-1) copies memory from a C address into Python bytes. See how it handles NUL terminators, binary data and unsafe pointers.
ctypes.addressof gives the integer address of a ctypes object's buffer. See how it differs from id, byref and pointer, with working examples.
Create mutable wchar_t buffers in Python with ctypes.create_unicode_buffer, and learn how wide character size differs on Windows, Linux and macOS.