I have a shared library with a function that takes a int ** like this:
void printarray(int **array, int n, int m)
{
int i, j;
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
printf("%d\n", array[i][j]);
}
I would like to call the function from Python withctypes. I have written the following to do this:
from ctypes import *
cdll.LoadLibrary("libssa.so")
libssa = CDLL("libssa.so")
a = [[1, 2, 3], [4, 5, 6]]
T = ((c_int * 3) * 2)
array = T()
for i in range(2):
for j in range(3):
array[i][j] = a[i][j]
libssa.printarray(array, c_int(3), c_int(2))
I expected that the type T would be an array of pointers to arrays of ints, and that array would be a pointer to an object of that type.
However, a segmentation fault occurs whenever an access to array is made in the C code. In particular, for this example, valgrind points to this line: printf("%d\n", array[i][j]); as the source of the segfault.
The question is, what is the proper way to construct a ctypes object that can be used as an argument of type int **?