How do I convert a Python list into a C array by using ctypes?

The following code works on arbitrary lists:

import ctypes
py_values = [1, 2, 3, 4]
arr = (ctypes.c_int * len(py_values))(*py_values)

Leave a Comment