# Calling a C function which return a pointer to struct

**URL:** <https://discourse.julialang.org/t/calling-a-c-function-which-return-a-pointer-to-struct/16409>\
**Category:** General Usage\
**Tags:** ccall\
**Created:** [October 16, 2018, 2:19pm UTC](https://discourse.julialang.org/t/calling-a-c-function-which-return-a-pointer-to-struct/16409 "2018-10-16T14:19:37Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hongliang\_Yang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hongliang_yang/32/8141_2.png) [@Hongliang\_Yang](https://discourse.julialang.org/u/Hongliang_Yang)\
**Post date:** [October 16, 2018, 2:19pm UTC](https://discourse.julialang.org/t/calling-a-c-function-which-return-a-pointer-to-struct/16409/1 "2018-10-16T14:19:37Z")

</div>

Hi Julia users,

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

```julia
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?

---

<div class="post-metadata">

**Author:** ![sdanisch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sdanisch/32/1406_2.png) [@sdanisch](https://discourse.julialang.org/u/sdanisch)\
**Post date:** [October 16, 2018, 3:12pm UTC](https://discourse.julialang.org/t/calling-a-c-function-which-return-a-pointer-to-struct/16409/2 "2018-10-16T15:12:35Z")

</div>

something like this should work:

```julia
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 😉

---

<div class="post-metadata">

**Author:** ![sdanisch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sdanisch/32/1406_2.png) [@sdanisch](https://discourse.julialang.org/u/sdanisch)\
**Post date:** [October 16, 2018, 3:15pm UTC](https://discourse.julialang.org/t/calling-a-c-function-which-return-a-pointer-to-struct/16409/3 "2018-10-16T15:15:50Z")

</div>

should be used like this in ccall:

```nohighlight
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[]

```

---

<div class="post-metadata">

**Author:** ![Hongliang\_Yang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hongliang_yang/32/8141_2.png) [@Hongliang\_Yang](https://discourse.julialang.org/u/Hongliang_Yang)\
**Post date:** [October 19, 2018, 9:27am UTC](https://discourse.julialang.org/t/calling-a-c-function-which-return-a-pointer-to-struct/16409/4 "2018-10-19T09:27:21Z")

</div>

Thank you. But how to get the value of _mapping\_to\_primitive_ in struct Dataset? The following code is not working，

```julia
unsafe_load(jl_dataset.mapping_to_primitive) 

```
