# Confusion about Ptr, Ref and dereferencing

**URL:** <https://discourse.julialang.org/t/confusion-about-ptr-ref-and-dereferencing/18805>\
**Category:** New to Julia\
**Tags:** ccall\
**Created:** [December 18, 2018, 7:56pm UTC](https://discourse.julialang.org/t/confusion-about-ptr-ref-and-dereferencing/18805 "2018-12-18T19:56:30Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![leethargo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leethargo/32/6004_2.png) [@leethargo](https://discourse.julialang.org/u/leethargo)\
**Post date:** [December 18, 2018, 7:56pm UTC](https://discourse.julialang.org/t/confusion-about-ptr-ref-and-dereferencing/18805/1 "2018-12-18T19:56:30Z")

</div>

I’m using `ccall` to access a C library that has its own memory management. For the constructor and destructor calls, it expects a `void**`. I thought it would be most natural to create a `Ptr{Cvoid}` in Julia, then pass a `Ref` of that to the `ccall`. However, I get this unexpected behavior:

```julia
julia> ptr = Ptr{Cvoid}(C_NULL)
Ptr{Nothing} @0x0000000000000000

julia> Ref(ptr)[] == ptr
true

julia> ptrptr = Ref(ptr)
Base.RefValue{Ptr{Nothing}}(Ptr{Nothing} @0x0000000000000000)

julia> ptrptr[] = 1
1

julia> ptrptr
Base.RefValue{Ptr{Nothing}}(Ptr{Nothing} @0x0000000000000001)

julia> ptrptr[]
Ptr{Nothing} @0x0000000000000001

julia> ptr
Ptr{Nothing} @0x0000000000000000

```

What I want to do in Julia should be equivalent to the follow C code:

```julia
void* ptr = NULL;
my_constructor(&ptr);
assert(ptr != NULL);
my_destructor(&ptr);

```

Is `Ref` not appropriate here? What should I do instead?

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [December 18, 2018, 8:06pm UTC](https://discourse.julialang.org/t/confusion-about-ptr-ref-and-dereferencing/18805/2 "2018-12-18T20:06:39Z")

</div>

What you want to do is impossible. If `ptr` is `C_NULL`, the only way to change that is by **assigning** to `ptr`, i.e. `ptr = ...`. There’s absolutely no exception to this rule, you cannot implicitly modify what a variable is bound to ever. Applying to immutable types (like `Ptr`) this means that without reassignment it’ll always be the same object witht he same content in that variable.

You already have the closest you can get in julia with your C code, which is to just use `ptrptr[]` instead.

---

<div class="post-metadata">

**Author:** ![leethargo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leethargo/32/6004_2.png) [@leethargo](https://discourse.julialang.org/u/leethargo)\
**Post date:** [December 18, 2018, 8:29pm UTC](https://discourse.julialang.org/t/confusion-about-ptr-ref-and-dereferencing/18805/3 "2018-12-18T20:29:21Z")

</div>

Thanks for your explanation.

So you suggest I never use my `ptr` variable in the Julia code at all and just do the following?

```julia
ptrptr = Ref(Ptr{Cvoid}(C_NULL))
my_constructor(ptrptr) # ptrptr[] = 1
use_it(ptrptr[])

```

I felt like there should be way of more directly translating the C code, but it looks like there isn’t.

I first read the section [When to use T, Ptr{T} and Ref{T}](https://docs.julialang.org/en/v1/manual/calling-c-and-fortran-code/#When-to-use-T,-Ptr%7BT%7D-and-Ref%7BT%7D-1) and did not fully understand. Now that I revisted it, I found another section further down: [Passing Pointers for Modifying Inputs](https://docs.julialang.org/en/v1/manual/calling-c-and-fortran-code/#Passing-Pointers-for-Modifying-Inputs-1). In the example there, the Julia local variables are also of `Ref` type already.

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [December 18, 2018, 8:38pm UTC](https://discourse.julialang.org/t/confusion-about-ptr-ref-and-dereferencing/18805/4 "2018-12-18T20:38:19Z")

</div>

> [@leethargo](#):
>
> just do the following?

Well, you can do `ptr = ptrptr[]` but otherwise basically yes.
