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

PythonCCtypes

Python Problem Overview


If I have the follow 2 sets of code, how do I glue them together?

void
c_function(void *ptr) {
    int i;

    for (i = 0; i < 10; i++) {
        printf("%p", ptr[i]);
    }

    return;
}


def python_routine(y):
    x = []
    for e in y:
        x.append(e)

How can I call the c_function with a contiguous list of elements in x? I tried to cast x to a c_void_p, but that didn't work.

I also tried to use something like

x = c_void_p * 10 
for e in y:
    x[i] = e

but this gets a syntax error.

The C code clearly wants the address of an array. How do I get this to happen?

Python Solutions


Solution 1 - Python

The following code works on arbitrary lists:

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

Solution 2 - Python

This is an explanation of the accepted answer.

ctypes.c_int * len(pyarr) creates an array (sequence) of type c_int of length 4 (python3, python 2). Since c_int is an object whose constructor takes one argument, (ctypes.c_int * len(pyarr)(*pyarr) does a one shot init of each c_int instance from pyarr. An easier to read form is:

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

Use type function to see the difference between seq and arr.

Solution 3 - Python

From the ctypes tutorial:

>>> IntArray5 = c_int * 5
>>> ia = IntArray5(5, 1, 7, 33, 99)

Solution 4 - Python

import ctypes
import typing

def foo(aqs : typing.List[int]) -> ctypes.Array:
    array_type = ctypes.c_int64 * len(aqs)
    ans = array_type(*aqs)
    return ans

for el in foo([1,2,3]):
    print(el)

this will give:

1
2
3

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNo One in ParticularView Question on Stackoverflow
Solution 1 - PythonGabi PurcaruView Answer on Stackoverflow
Solution 2 - PythonakhanView Answer on Stackoverflow
Solution 3 - PythonRyan GinstromView Answer on Stackoverflow
Solution 4 - PythonJörg BeyerView Answer on Stackoverflow