gpt4 book ai didi

python - 在 Python 中创建二维 c_types 数组并在 C 函数中使用它

转载 作者:太空宇宙 更新时间:2023-11-04 01:48:30 24 4
gpt4 key购买 nike

我在 Google 上搜索过,但找不到解决方案。我只想创建一个 2D 指针数组,它引用现有的 python 数组,以便在 C 函数中发送它,这要归功于 c_types。

tab 是一个现有的一维数组,它有效:

arr = (c_int * 1000000)(*tab)
Basic.Basic_c.quicksort.restype = POINTER(c_int )
c = Basic.Basic_c.quicksort(arr)

二维数组:

Matrix = [[0 for x in range(8)] for y in range(5)];
arr = ((c_int * 8)*5)(*Matrix)

那没有用,创建一个错误:

TypeError: expected c_long_Array_8 instance, got list

我找不到解决方案。

最佳答案

您可以使用元组的元组,而不是使用 2d 列表。因此,在创建矩阵后,将其转换为元组,如下所示。

Matrix = [[0 for x in range(8)] for y in range(5)];
Matrix = tuple(map(tuple, Matrix))
arr = ((c_int * 8)*5)(*Matrix)

关于python - 在 Python 中创建二维 c_types 数组并在 C 函数中使用它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58583674/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com