当前位置:主页 > 软件编程 > Python代码 >

利用ctypes获取numpy数组的指针方法

时间:2021-03-11 10:08:53 | 栏目:Python代码 | 点击:

如下所示:

import numpy as np
from ctypes import *

a = np.asarray(range(16), dtype=np.int32).reshape([4,4])
if not a.flags['C_CONTIGUOUS']:
  a = np.ascontiguous(a, dtype=a.dtype) # 如果不是C连续的内存,必须强制转换
a_ctypes_ptr = cast(a.ctypes.data, POINTER(c_int))  #转换为ctypes,这里转换后的可以直接利用ctypes转换为c语言中的int*,然后在c中使用
for i in range(16):
  print(a_ctypes_ptr[i])

您可能感兴趣的文章:

相关文章