什么是使用ctypes将numpy 2d数组传递给c函数的正确方法?以下是我目前的尝试(导致了段错误):
C代码:
C代码:
void test(double **in_array, int N) {
int i, j;
for(i = 0; i<N; i++) {
for(j = 0; j<N; j++) {
printf("%e \t", in_array[i][j]);
}
printf("\n");
}
}
Python 代码:
from ctypes import *
import numpy.ctypeslib as npct
array_2d_double = npct.ndpointer(dtype=np.double,ndim=2, flags='CONTIGUOUS')
liblr = npct.load_library('libtest.so', './src')
liblr.test.restype = None
liblr.test.argtypes = [array_2d_double, c_int]
x = np.arange(100).reshape((10,10)).astype(np.double)
liblr.test(x, 10)
double **
和double [N][N]
是不能互换的吗? - Filipe Gonçalvesin_array
声明为double (*in_array)[N]
,其中N是第二维的大小。 - Filipe Gonçalves