passing c++ double pointer to python
Here's a way. I didn't see a nice way to use numpy with double**.
test.cpp (Windows)
#include <stdio.h>extern "C" __declspec(dllexport) void cfun(const double ** indata, int rowcount, int colcount, double ** outdata) { for (int i = 0; i < rowcount; ++i) { for (int j = 0; j < colcount; ++j) { outdata[i][j] = indata[i][j] * 4; } }}
test.py
import numpyimport ctypes# Allocate array of double*indata = (ctypes.POINTER(ctypes.c_double) * 5)()for i in range(5): # Allocate arrays of double indata[i] = (ctypes.c_double * 6)() for j in range(6): indata[i][j] = 1.0outdata = (ctypes.POINTER(ctypes.c_double) * 5)()for i in range(5): outdata[i] = (ctypes.c_double * 6)() for j in range(6): outdata[i][j] = 1.0lib = ctypes.cdll.LoadLibrary('test')fun = lib.cfundef dump(a,rows,cols): for i in range(rows): for j in range(cols): print a[i][j], printdump(indata,5,6)fun(ctypes.byref(indata),5,6,ctypes.byref(outdata))dump(outdata,5,6)
Output
1.0 1.0 1.0 1.0 1.0 1.01.0 1.0 1.0 1.0 1.0 1.01.0 1.0 1.0 1.0 1.0 1.01.0 1.0 1.0 1.0 1.0 1.01.0 1.0 1.0 1.0 1.0 1.04.0 4.0 4.0 4.0 4.0 4.04.0 4.0 4.0 4.0 4.0 4.04.0 4.0 4.0 4.0 4.0 4.04.0 4.0 4.0 4.0 4.0 4.04.0 4.0 4.0 4.0 4.0 4.0