If I have the follow 2 sets of code, how do I glue them together?
JavaScript
x
17
17
1
void
2
c_function(void *ptr) {
3
int i;
4
5
for (i = 0; i < 10; i++) {
6
printf("%p", ptr[i]);
7
}
8
9
return;
10
}
11
12
13
def python_routine(y):
14
x = []
15
for e in y:
16
x.append(e)
17
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
JavaScript
1
4
1
x = c_void_p * 10
2
for e in y:
3
x[i] = e
4
but this gets a syntax error.
The C code clearly wants the address of an array. How do I get this to happen?
Advertisement
Answer
The following code works on arbitrary lists:
JavaScript
1
4
1
import ctypes
2
py_values = [1, 2, 3, 4]
3
arr = (ctypes.c_int * len(py_values))(*py_values)
4