Calling a C function which return a pointer to struct

Hi Julia users,

I want to call a C function which return a pointer to struct, such as

typedef struct {
  int n_atoms;
  int *mapping_to_primitive;
  double transformation_matrix[3][3];
  double origin_shift[3];
  int (*rotations)[3][3];
  double (*translations)[3];
} Dataset;

How to define this in Julia?

something like this should work:

struct Dataset
    n_atoms::Cint
    mapping_to_primitive::Ptr{Cint}
    # TODO check if row/column major
    transformation_matrix::SMatrix{Float64, 3, 3} 
    origin_shift::SVector{Float64, 3}
    # TODO not sure if I understand the syntax correctly for int (*rotations)[3][3];
    rotations::Ptr{SMatrix{Cint, 3, 3}} 
    translations::Ptr{SVector{Float64, 3}}
end

Might need some more investigation :wink:

1 Like

should be used like this in ccall:

ptr = ccall((:func, "lib"), Ptr{Dataset}, (...), ...)
jl_dataset = unsafe_load(ptr)
# this might also work (untested)
ptr = ccall((:func, "lib"), Ref{Dataset}, (...), ...)
jl_dataset = ptr[]
2 Likes

Thank you. But how to get the value of mapping_to_primitive in struct Dataset? The following code is not working,

unsafe_load(jl_dataset.mapping_to_primitive)