The background
I'm trying to use the ccall
Julia function to use code written in C.
I know how to pass an array as an argument to a function that expects int *arg
. For example, trying to use this C function
void sum_one(int *arr, int len)
{
for (int i=0; i<len; i++){
arr[i]++;
}
}
this Julia code works
x = collect(Cint, 1:5)
ccall((:sum_one, "/path/to/mylib.so"), Void, (Ptr{Cint}, Cint), x, 5)
The problem
It doesn't seem to be so straight forward with C functions that expect a pointer to a pointer (int **arg
) to be used as a 2-dimensional matrix. Say this one
void fill_matrix(int **arr, int row, int col)
{
for (int i=0; i<row; i++){
for (int j=0; j<col; j++){
arr[i][j] = arr[i][j] + i + j*10;
}
}
}
Here, I needed to create an Array of Arrays so that the C code would accept it:
xx = [zeros(Cint, 5) for i in 1:6]
ccall((:fill_matrix, "/path/to/mylib.so"),
Void, (Ptr{Ptr{Cint}}, Cint, Cint), xx, 6,5)
But this structure structure is not very convenient from the Julia side.
The question(s)
- Is there any other way to pass a 2-dimensional matrix to a C function that expects an argument of the type
int **arg
? - If not, how can you transform an already existing 2-dimensional array of Julia to the array of arrays structure of C?
- and the other way around?