如何在Python中使用C中的float **?

发布于 2024-09-05 20:36:59 字数 338 浏览 4 评论 0原文

在我的问题 How to use float ** in Python 没有成功之后与 Swig?,我开始认为 Swig 可能不是首选武器。我需要一些 c 函数的绑定。这些函数之一需要一个浮点数**。你会推荐什么? C型?

接口文件:

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

after having no success with my question on How to use float ** in Python with Swig?, I started thinking that swig might not be the weapon of choice. I need bindings for some c functions. One of these functions takes a float**. What would you recomend? Ctypes?

Interface file:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

楠木可依 2024-09-12 20:36:59

我现在已经在几个项目中使用了 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)

I've used ctypes for several projects now and have been quite happy with the results. I don't think I've personally needed a pointer-to-pointer wrapper yet but, in theory, you should be able to do the following:

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文