# Passing an array of structures through \`ccall\`, clarification

**URL:** <https://discourse.julialang.org/t/passing-an-array-of-structures-through-ccall-clarification/8151>\
**Category:** General Usage\
**Tags:** ccall\
**Created:** [January 4, 2018, 9:59am UTC](https://discourse.julialang.org/t/passing-an-array-of-structures-through-ccall-clarification/8151 "2018-01-04T09:59:46Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![lobingera](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lobingera/32/211_2.png) [@lobingera](https://discourse.julialang.org/u/lobingera)\
**Post date:** [January 4, 2018, 9:59am UTC](https://discourse.julialang.org/t/passing-an-array-of-structures-through-ccall-clarification/8151/1 "2018-01-04T09:59:46Z")

</div>

I tried to follow the discussion in

> [@Passing an array of structures through \`ccall\`](https://discourse.julialang.org/t/passing-an-array-of-structures-through-ccall/5194):
>
> I’m trying to pass an array of structure (actually, a pointer to the chunk of memory containing the first structure) from Julia to C through ccall. An example of what I’m trying to do is shown below, using just C: #include "stdio.h" #include "string.h" #include "stdlib.h" //Define a test structure struct st\_test { int i; double d; char s[10]; double a[2]; int b[2]; }; //Print the contents of an array of structure, and modify their values //upon return. int printAndModify…

because i need to solve a similar problem: C-API expects an array of struct.

Do i understand correctly, that there are exactly two options:

- write glue code in C that interfaces a julia type
- rearrange the data in julia (manually, mostly in a loop, i’d guess it looks like [https://github.com/JuliaGraphics/Cairo.jl/blob/master/src/Cairo.jl#L771](https://github.com/JuliaGraphics/Cairo.jl/blob/master/src/Cairo.jl#L771)

---

<div class="post-metadata">

**Author:** ![ihnorton](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ihnorton/32/26_2.png) [@ihnorton](https://discourse.julialang.org/u/ihnorton)\
**Post date:** [January 4, 2018, 6:46pm UTC](https://discourse.julialang.org/t/passing-an-array-of-structures-through-ccall-clarification/8151/2 "2018-01-04T18:46:58Z")

</div>

Does this help?

```julia
julia> struct Foo
         x::Int64
         y::Int64
       end

julia> a = Foo[Foo(1,3), Foo(2,4), Foo(3,5)]
3-element Array{Foo,1}:
 Foo(1, 3)
 Foo(2, 4)
 Foo(3, 5)

julia> p = pointer(a) # don't do this
Ptr{Foo} @0x00000001058f4e50

julia> unsafe_load(p)
Foo(1, 3)

julia> unsafe_load(p+sizeof(Foo))
Foo(2, 4)

julia> unsafe_load(p+sizeof(Foo)*2)
Foo(3, 5)

```

---

<div class="post-metadata">

**Author:** ![lobingera](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lobingera/32/211_2.png) [@lobingera](https://discourse.julialang.org/u/lobingera)\
**Post date:** [January 4, 2018, 7:30pm UTC](https://discourse.julialang.org/t/passing-an-array-of-structures-through-ccall-clarification/8151/3 "2018-01-04T19:30:58Z")

</div>

Maybe. The question was rather, if in the meanwhile something as macros etc. showed up.  
My example is a c struct with different c -types → Long + 2 Doubles (so 4bytes and 2\*8bytes) and i learned that you should not expect julia to allocate in consecutive memory.

---

<div class="post-metadata">

**Author:** ![simonbyrne](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simonbyrne/32/19_2.png) [@simonbyrne](https://discourse.julialang.org/u/simonbyrne)\
**Post date:** [January 4, 2018, 7:42pm UTC](https://discourse.julialang.org/t/passing-an-array-of-structures-through-ccall-clarification/8151/4 "2018-01-04T19:42:05Z")

</div>

As long as you know how the macros expand (I assume you’re talking about C macros here), then you shouldn’t have a problem, as Julia structs should match C structs in layout.

The cairo hacks are to work around C unions, which can’t be represented by Julia at the moment.

---

<div class="post-metadata">

**Author:** ![ihnorton](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ihnorton/32/26_2.png) [@ihnorton](https://discourse.julialang.org/u/ihnorton)\
**Post date:** [January 4, 2018, 7:53pm UTC](https://discourse.julialang.org/t/passing-an-array-of-structures-through-ccall-clarification/8151/5 "2018-01-04T19:53:47Z")

</div>

I think there might be some confusion here, so maybe it helps to be specific about the points of discussion and the struct layout…

- The `struct` below has the same layout as such a struct in C.

```julia
struct LobingerAStruct
  x::Clong
  y::Cdouble
  z::Cdouble
end

```

- The point of the example I shared is that you can store these non-mutable `struct` objects in an Array and treat the memory as consecutive (respecting the usual `ccall` memory rules for passing arrays).

- I think the point of the macros you are referring to is to make working with immutable `struct`s easier in general, but that is a bit orthogonal to whether it is possible to pass an array of structs to C without a shim.

---

<div class="post-metadata">

**Author:** ![lobingera](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lobingera/32/211_2.png) [@lobingera](https://discourse.julialang.org/u/lobingera)\
**Post date:** [January 4, 2018, 10:46pm UTC](https://discourse.julialang.org/t/passing-an-array-of-structures-through-ccall-clarification/8151/6 "2018-01-04T22:46:49Z")

</div>

Thank you, this looks much friendlier now. I got confused by your example having two equal Julia types as entries - and the “don’t do this” comment …

---

<div class="post-metadata">

**Author:** ![ihnorton](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ihnorton/32/26_2.png) [@ihnorton](https://discourse.julialang.org/u/ihnorton)\
**Post date:** [January 5, 2018, 2:26am UTC](https://discourse.julialang.org/t/passing-an-array-of-structures-through-ccall-clarification/8151/7 "2018-01-05T02:26:48Z")

</div>

> [@lobingera](#):
>
> the “don’t do this” comment

Sorry – what I meant was calling `pointer` was for demo purposes; for real use-cases you should pass the array as indicated in the `ccall` manual.

---

<div class="post-metadata">

**Author:** ![lobingera](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lobingera/32/211_2.png) [@lobingera](https://discourse.julialang.org/u/lobingera)\
**Post date:** [January 6, 2018, 7:03pm UTC](https://discourse.julialang.org/t/passing-an-array-of-structures-through-ccall-clarification/8151/8 "2018-01-06T19:03:21Z")

</div>

> [@ihnorton](#):
>
> pass the array as indicated in the ccall manual.

raises a little bit the quesion about where and in which manual?  
btw:

```_
   _ _ _(_)_ | A fresh approach to technical computing
  (_) | (_) (_) | Documentation: https://docs.julialang.org
   _ _ _| |_ __ _ | Type "?help" for help.
  | | | | | | |/ _` | |
  | | |_| | | | (_| | | Version 0.6.2 (2017-12-13 18:08 UTC)
 _/ |\ __'_|_|_|\__'_| | Official http://julialang.org/ release
|__/ | x86_64-pc-linux-gnu

julia> type glyph_t
               index::Clong
               x::Cdouble
               y::Cdouble
       end

julia> gf = [glyph_t(61,0,0), glyph_t(81,31,0)]
2-element Array{glyph_t,1}:
 glyph_t(61, 0.0, 0.0) 
 glyph_t(81, 31.0, 0.0)

julia> p = pointer(gf)
Ptr{glyph_t} @0x00007f3aa321ca10

julia> unsafe_load(p)
glyph_t(139889821507696, 6.91147550098323e-310, 0.0)

```

is this expected?

---

<div class="post-metadata">

**Author:** ![favba](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/favba/32/2735_2.png) [@favba](https://discourse.julialang.org/u/favba)\
**Post date:** [January 6, 2018, 7:38pm UTC](https://discourse.julialang.org/t/passing-an-array-of-structures-through-ccall-clarification/8151/9 "2018-01-06T19:38:34Z")

</div>

Isn’t `type` a deprecated alias for `mutable struct`? I belive you want an immutable, a `struct`.

---

<div class="post-metadata">

**Author:** ![lobingera](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lobingera/32/211_2.png) [@lobingera](https://discourse.julialang.org/u/lobingera)\
**Post date:** [January 6, 2018, 7:41pm UTC](https://discourse.julialang.org/t/passing-an-array-of-structures-through-ccall-clarification/8151/10 "2018-01-06T19:41:55Z")

</div>

(facepalm!)

Yes, i do.
