Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Python中使用C中的浮点**?_Python_C_Pointers_Swig_Ctypes - Fatal编程技术网

如何在Python中使用C中的浮点**?

如何在Python中使用C中的浮点**?,python,c,pointers,swig,ctypes,Python,C,Pointers,Swig,Ctypes,在我的问题没有成功之后,我开始认为swig可能不是首选的武器。我需要一些c函数的绑定。其中一个函数采用浮点**。你会推荐什么?Ctypes 接口文件: extern int read_data(const char *file,int *n_,int *m_,float **data_,int **classes_); 我现在已经在几个项目中使用了ctypes,并且对结果非常满意。我个人并不需要指针包装器,但理论上,您应该能够做到以下几点: from ctypes import * your

在我的问题没有成功之后,我开始认为swig可能不是首选的武器。我需要一些c函数的绑定。其中一个函数采用浮点**。你会推荐什么?Ctypes

接口文件:

extern int read_data(const char *file,int *n_,int *m_,float **data_,int **classes_);

我现在已经在几个项目中使用了
ctypes
,并且对结果非常满意。我个人并不需要指针包装器,但理论上,您应该能够做到以下几点:

from ctypes import *

your_dll = cdll.LoadLibrary("your_dll.dll")

PFloat = POINTER(c_float)
PInt   = POINTER(c_int)

p_data    = PFloat()
p_classes = PInt()
buff      = create_string_buffer(1024)
n1        = c_int( 0 )
n2        = c_int( 0 )

ret = your_dll.read_data( buff, byref(n1), byref(n2), byref(p_data), byref(p_classes) )

print('Data:    ', p_data.contents)
print('Classes: ', p_classes.contents)

@Rakis,如果你把这个答案也贴到我的另一个问题“如何在Python中使用float**with Swig?”(参见“链接”部分),我也会接受你的答案;