栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

ctypes包含数组的结构

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

ctypes包含数组的结构

代码有2个问题(如我在评论中所述):

  1. print_array_struct.argtype- 不正确
  2. Ç 的阵列 为主,而在 Python中 它们是 ctypes.c_intINT )的基础

有关更多详细信息,请参见[Python 3.Docs]:ctypes-
Python的外部函数库

我修改了您的 Python 代码,以纠正上述错误(以及其他一些小问题)。

pre00.py

#!/usr/bin/env python3import sysimport ctypesDLL_NAME = "./my_so_object.so"DOUBLE_10 = ctypes.c_double * 10class ArrayStruct(ctypes.Structure):    _fields_ = [        ("first_array", DOUBLE_10),        ("second_array", DOUBLE_10),    ]def main():    dll_handle = ctypes.CDLL(DLL_NAME)    print_array_struct_func = dll_handle.print_array_struct    print_array_struct_func.argtypes = [ArrayStruct]    print_array_struct_func.restype = None    x1 = DOUBLE_10()    x2 = DOUBLE_10()    x1[:] = range(1, 11)    x2[:] = range(11, 21)    print([item for item in x1])    print([item for item in x2])    arg = ArrayStruct(x1, x2)    print_array_struct_func(arg)if __name__ == "__main__":    print("Python {:s} on {:s}n".format(sys.version, sys.platform))    main()

输出

[cfati@cfati-ubtu16x64-0:~/Work/Dev/StackOverflow/q050447199]> python3

pre00.py
Python 3.5.2 (default, Nov 23 2017, 16:37:01)
[GCC 5.4.0 20160609] on linux

[1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0][11.0, 12.0, 13.0, 14.0, 15.0, 16.0, 17.0, 18.0, 19.0, 20.0]1.0000002.0000003.0000004.0000005.0000006.0000007.0000008.0000009.00000010.000000

更新 #0

错误 #1。 是( 较新的
)[SO]的“重复”
:通过ctypes从Python调用的C函数返回错误的值(@CristiFati的回答)。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/612016.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号