【发布时间】:2020-03-02 19:20:56
【问题描述】:
我正在尝试使用 ctypes 将二维数组从 Python 传递到 C。 数组 dtype 是 uint16。 我写了一个简单的代码来了解它是如何工作的:
C:
#include <stdint.h>
__declspec(dllexport) uint16_t Test(uint16_t **arr)
{
return (arr[5][5]);
}
Python:
import numpy as np
from ctypes import cdll
import ctypes
from numpy.ctypeslib import ndpointer
_p = ndpointer(dtype=np.uint16, ndim=2, shape=(10, 10), flags='C')
mydll = cdll.LoadLibrary("mydll.dll")
_func = mydll.Test
_func.argtypes = [_p]
_func.restypes = ctypes.c_uint16
data = np.empty([10, 10], dtype=np.uint16)
data[5, 5] = 5
print(_func(data))
我得到 OSError: 访问冲突读取 0xFFFFFFFFFFFFFFFFFFFFFF 我做错了什么,我该如何解决?
【问题讨论】:
标签: python c arrays pointers ctypes