I am experimenting with calls to C functions, and got a bit puzzled when using multi-dimensional arrays as parameters.
I have this bit of C code in cfile.c
:
void f(double array[2][2])
{
array[1][1] = 42.;
return;
}
which I compile with gcc -fPIC -c cfile.c && gcc -shared -fPIC cfile.o -o cfile.so
. Then I import the function in Julia :
using Libdl
lib = Libdl.dlopen("./cfile.so")
func = Libdl.dlsym(lib, :f)
a = rand(Cdouble, 2, 2)
ccall(func, Cvoid, (Ref{Ref{Cdouble}}, ), a)
println(a)
Libdl.dlclose(lib)
Now with this bit of code, a
doesn’t change. However, if I use (Ptr{Cvoid}, )
as parameter type in the ccall
, it works as intended. And if I use Ptr{Ptr{Cdouble}}
, I get an error:
ERROR: LoadError: MethodError: no method matching unsafe_convert(::Type{Ptr{Float64}}, ::Float64)
Is there something I don’t do right? Or should I simply use Ptr{Cvoid}
with multi-dimensional arrays?