我如何转换成一个Python列表成C数组使用ctypes的? [英] How do I convert a Python list into a C array by using ctypes?
本文介绍了我如何转换成一个Python列表成C数组使用ctypes的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果我有后续的2套code的,我怎么粘合在一起呢?
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)
我怎么能说与x的元素的连续名单c_function?我试着投x与c_void_p,但没有奏效。
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.
我也尝试使用类似
x = c_void_p * 10
for e in y:
x[i] = e
但得到一个语法错误。
but this gets a syntax error.
的C code显然想要一个数组的地址。我如何做到这一点?
The C code clearly wants the address of an array. How do I get this to happen?
推荐答案
以下code适用于任意列表:
The following code works on arbitrary lists:
import ctypes
arr = (ctypes.c_int * len(pyarr))(*pyarr)
这篇关于我如何转换成一个Python列表成C数组使用ctypes的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文