# Dereferencing an Array of Struct Pointer

**URL:** <https://discourse.julialang.org/t/dereferencing-an-array-of-struct-pointer/51145>\
**Category:** General Usage\
**Tags:** question\
**Created:** [December 2, 2020, 8:01pm UTC](https://discourse.julialang.org/t/dereferencing-an-array-of-struct-pointer/51145 "2020-12-02T20:01:01Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Brad\_Carman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brad_carman/32/17631_2.png) [@Brad\_Carman](https://discourse.julialang.org/u/Brad_Carman)\
**Post date:** [December 2, 2020, 8:01pm UTC](https://discourse.julialang.org/t/dereferencing-an-array-of-struct-pointer/51145/1 "2020-12-02T20:01:01Z")

</div>

I’m having trouble dereferencing a pointer to an array of structs. See the following code for an example:

```julia
mutable struct Test
    x::Float64
end

tests = [Test(1.2), Test(3.4)]

tests_ptr = Base.unsafe_convert(Ptr{Test}, tests)

test1 = unsafe_load(tests_ptr)
test2 = unsafe_load(tests_ptr + sizeof(Test))

println("test1.x = $(test1.x)")
println("test2.x = $(test2.x)")

```

The result I get is junk:

```julia
test1.x = 1.719804727e-315
test2.x = 1.719804806e-315

```

My goal in the end is to pass data using structures back and forth from c#, but first I need to figure out how to get the correct pointer to each element of the struct array.

---

<div class="post-metadata">

**Author:** ![FedericoStra](https://avatars.discourse-cdn.com/v4/letter/f/76d3ee/32.png) [@FedericoStra](https://discourse.julialang.org/u/FedericoStra)\
**Post date:** [December 2, 2020, 8:22pm UTC](https://discourse.julialang.org/t/dereferencing-an-array-of-struct-pointer/51145/2 "2020-12-02T20:22:36Z")

</div>

There are the unsafe function

```julia
pointer(tests, i)

```

and the safe function

```julia
Ref(tests, i)

```

to get respectively a pointer and a reference to the `i`-th element of `tests`.

---

<div class="post-metadata">

**Author:** ![Brad\_Carman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brad_carman/32/17631_2.png) [@Brad\_Carman](https://discourse.julialang.org/u/Brad_Carman)\
**Post date:** [December 2, 2020, 8:24pm UTC](https://discourse.julialang.org/t/dereferencing-an-array-of-struct-pointer/51145/3 "2020-12-02T20:24:23Z")

</div>

This still gives back junk…

```julia
julia> pointer(tests, 1)
Ptr{Test} @0x0000000015d32270

julia> tests_ptr
Ptr{Test} @0x0000000015d32270

julia> unsafe_load(pointer(tests,1))
Test(1.795226654e-315)

```

---

<div class="post-metadata">

**Author:** ![FedericoStra](https://avatars.discourse-cdn.com/v4/letter/f/76d3ee/32.png) [@FedericoStra](https://discourse.julialang.org/u/FedericoStra)\
**Post date:** [December 2, 2020, 8:31pm UTC](https://discourse.julialang.org/t/dereferencing-an-array-of-struct-pointer/51145/4 "2020-12-02T20:31:56Z")

</div>

Uhm… You are right. The reason is that in the array `tests` there are not stored the objects `Test` themselves, but just pointers to them, so there is a double indirection (because they are mutable, which I didn’t notice).

The following works, but seem _extremely fragile_, so there must be a better solution:

```julia
p = Base.unsafe_convert(Ptr{Ptr{Test}}, pointer(tests, 1))
unsafe_load(unsafe_load(p))

```

**Edit**

This works as well and is a bit cleaner:

```julia
p = Base.unsafe_convert(Ptr{Test}, Ref(tests, 1))
unsafe_load(p)

```

---

<div class="post-metadata">

**Author:** ![Brad\_Carman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brad_carman/32/17631_2.png) [@Brad\_Carman](https://discourse.julialang.org/u/Brad_Carman)\
**Post date:** [December 2, 2020, 8:36pm UTC](https://discourse.julialang.org/t/dereferencing-an-array-of-struct-pointer/51145/5 "2020-12-02T20:36:50Z")

</div>

Thanks so much! That works!

---

<div class="post-metadata">

**Author:** ![FedericoStra](https://avatars.discourse-cdn.com/v4/letter/f/76d3ee/32.png) [@FedericoStra](https://discourse.julialang.org/u/FedericoStra)\
**Post date:** [December 2, 2020, 8:38pm UTC](https://discourse.julialang.org/t/dereferencing-an-array-of-struct-pointer/51145/6 "2020-12-02T20:38:40Z")

</div>

Yes it does, but I’m not so sure it is the most _Julianic_ way to achieve it, as I’m not too familiar with all the low level tools that the language provides to interface with external C code.

Maybe someone else can provide a cleaner solution that avoids `unsafe_convert`, I don’t know.

---

<div class="post-metadata">

**Author:** ![Brad\_Carman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brad_carman/32/17631_2.png) [@Brad\_Carman](https://discourse.julialang.org/u/Brad_Carman)\
**Post date:** [December 2, 2020, 9:24pm UTC](https://discourse.julialang.org/t/dereferencing-an-array-of-struct-pointer/51145/7 "2020-12-02T21:24:18Z")

</div>

Just for further info, here is the working code to get both elements:

```julia
mutable struct Test
    x::Float64
end

tests = [Test(1.2), Test(3.4)]

# ccall method
tests_ptr_c = @ccall jl_array_ptr(tests::Array{Test})::Ptr{Ptr{Test}}

# julia method
tests_ptr_j = Base.unsafe_convert(Ptr{Ptr{Test}}, tests)

test1_ptr = unsafe_load(tests_ptr_j)
test2_ptr = unsafe_load(tests_ptr_j+sizeof(Test))

test1 = unsafe_load(test1_ptr)
test2 = unsafe_load(test2_ptr)

println("test1.x = $(test1.x)")
println("test2.x = $(test2.x)")

```

Which gives the correct result:

```julia
test1.x = 1.2
test2.x = 3.4

```
