ctypes.ARRAY(type, length) creates a fixed-size array type of C values, such as five c_int items. In modern code, the documented and preferred way to write the same thing is multiplication: ctypes.c_int * 5. Both give you a class that you instantiate to get a C-compatible array you can pass to native functions.
Unlike a Python list, a ctypes array has a fixed length and a single element type, and its memory layout matches a C array.
Types are cached, so both spellings return the same class. The size is 20 bytes because five 4-byte c_int values fit end to end on common platforms. Use the type * length form in new code. It reads like C, it is what the documentation shows, and it works for every ctypes type.
Calling the array type creates an instance, zero-initialised by default. You can pass initial values positionally.
Missing values stay zero, and len(filled) is always the declared length. Passing more initial values than the length raises IndexError.
To build an array from an existing Python list, unpack it:
Arrays support indexing, negative indexes, slicing and iteration. Assignment converts Python values to the C type.
Python checks bounds for array objects, but when you hand the memory to C, nothing stops the C code from writing past the end. Always pass the length along with the buffer.
An array decays to a pointer to its first element when passed to a C function, as it does in C. This example calls qsort from the C library with a Python comparison callback.
The callback receives pointers, and a[0] reads the first element each pointer refers to. Subtraction can overflow for extreme values, so for general data compare with (a[0] > b[0]) - (a[0] < b[0]).
Array types can be fields of a Structure, and you can nest them for two dimensions.
Note the order: (c_int * 3) * 2 is two rows of three, so m[row][col].
bytes(arr) copies the raw bytes. bytearray and memoryview(arr) work too, because ctypes arrays support the buffer protocol.(ctypes.c_ubyte * n).from_buffer(bytearray_obj) shares memory with a writable buffer without copying.numpy.frombuffer(arr, dtype=np.int32) wraps the same memory without a copy.A ctypes array stores its elements contiguously in one C buffer, so a large c_double * 1_000_000 array uses about 8 MB and avoids the per-item object overhead of a Python list. Indexing from Python is still slower than C, because each access creates a Python object. For heavy numeric loops, hand the whole buffer to C code or wrap it with numpy.frombuffer, and keep the Python-level work to setup and results.
They produce the same cached array type. type * length is the documented, idiomatic form, so prefer it.
No. The length is fixed at type creation. Create a new array, or allocate a larger one up front.
Use list(arr), or slice it with arr[:]. For raw bytes, use bytes(arr).
No. An array owns its memory and has a size. A pointer only stores an address. An array converts to a pointer when passed to a C function.
ctypes.ARRAY(type, length) and type * length build fixed-size C array types. Create instances with optional initial values, index them like lists, pass them straight to C functions, and nest them in structures. Prefer the multiplication syntax and keep the length with the buffer whenever C code touches it.