我现在正在和ctypes做斗争。我可以把一个python列表转换成一个浮点数组,然后把它给C函数。但是我不知道如何把这个数组从C函数返回到python列表...
Python代码
class Point(ctypes.Structure):
_fields_= [("a", ctypes.c_float * 4),
("aa", ctypes.c_int)]
floats = [1.0, 2.0, 3.0, 4.0]
FloatArray4 = (ctypes.c_float * 4)
parameter_array = FloatArray4(*floats)
test1 = clibrary.dosth
test1.argtypes = [ctypes.c_float * 4, ctypes.c_int]
test1.restype = ctypes.POINTER(Point)
struc = test1(parameter_array, 9)
p = (struc.contents.a)
print(p)
clibrary.free_memory(struc)
C函数基本上是将parameter_array放入一个结构中,然后返回该结构。C代码:
#include <stdio.h>
#include <stdlib.h>
struct a{float *a;
int aa;
} ;
struct a *dosth(float *lsit, int x){
struct a *b = malloc(200000);
b -> a = lsit;
b -> aa = 3;
return b;
}
void free_memory(struct a *pointer){
free(pointer);
}
Python中print(p)的输出是:
<__main__.c_float_Array_4 object at 0x000001FE9EEA79C0>
如何访问这些值?
1条答案
按热度按时间o3imoua41#
对
ctypes
指针进行切片将生成一个内容的Python列表。由于指针不知道它所指向的项有多少,因此需要知道其大小,通常通过另一个参数:下面是一个基于您的代码的充实示例:
测试.c
测试.py
输出量: