I am expanding the Julia package, which uses the C library. I need to call some C functions from Julia. They look something like this:
struct contained { int x; int y; int z; }; struct mystruct { int n; contained* arr; }; mystruct* mk_mystruct(int n, contained* arr); void use_mystruct(mystruct* foo);
I also declared the corresponding types in Julia:
type contained x::Int64 y::Int64 z::Int64 end type mystruct n::Int64 arr::Array{contained, 1} end
In ccall functions that take contained* as an argument, everything works fine, treating contained* as Ptr{Int64} :
con = fill(0, 5, 3); mys = ccall((:mk_mystruct, "mylib"), Ptr{mystruct}, (Int64, Ptr{Int64}), n, con)
I suppose this works because contained has the same memory layout as the Int64 array. This is also how it is done elsewhere in the Julia package. But the only way to know the value of the returned mystruct is to dereference it with unsafe_load , after which Julia flies out of segfault. What is the correct way to dereference a pointer in Julia?
The C library also includes pretty print functions, so instead of dereferencing a pointer in Julia, I could treat the pointer as opaque and pass it to this C function:
void print_mystruct(mystruct* foo, FILE* outputfile)
In C code, this is called using outputfile=stdout . How would I install this using ccall ? This clearly does not work:
ccall((:print_mystruct, "mylib"), Void, (Ptr{mystruct}, Ptr{Void}), mys, stdout)
What should I put in place of Ptr{Void} and stdout ? How does Julia implement I / O in the C interface?