我试图将Numpy数组传递给C,但是在Windows和Linux中获得不同的结果。
在Python中
import platform
import numpy as np
import ctypes
if platform.system() == 'Windows':
c_fun = np.ctypeslib.load_library("/mypath/c_fun.dll",".").c_fun
else: # Linux
c_fun = np.ctypeslib.load_library("/mypath/c_fun.so",".").c_fun
c_fun.argtypes = [np.ctypeslib.ndpointer(dtype=np.int,ndim=2,flags="C_CONTIGUOUS"),ctypes.c_int,ctypes.c_int]
array = np.array([[0,1,0],[0,0]])
rows,cols = array.shape
c_fun(array,rows,cols)
在C
void c_fun(int* array,int rows,int cols)
{
for (int i = 0; i < rows * cols; i++)
printf("%d ",array[i]);
}
当我在Windows中运行该程序时,输出为“ 0 1 0 0 1 0 0 0 1 0”,效果很好。
但是在Linux中,输出为“ 0 0 1 0 0 0 0 0 0 1”,为什么?