# Clarification on the differences between \`Ptr\` and \`Ref\`

**URL:** <https://discourse.julialang.org/t/clarification-on-the-differences-between-ptr-and-ref/35531>\
**Category:** General Usage\
**Created:** [March 4, 2020, 5:54pm UTC](https://discourse.julialang.org/t/clarification-on-the-differences-between-ptr-and-ref/35531 "2020-03-04T17:54:09Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![kdheepak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kdheepak/32/10881_2.png) [@kdheepak](https://discourse.julialang.org/u/kdheepak)\
**Post date:** [March 4, 2020, 5:54pm UTC](https://discourse.julialang.org/t/clarification-on-the-differences-between-ptr-and-ref/35531/1 "2020-03-04T17:54:09Z")

</div>

I have a question regarding `ccall`, `Ptr`, `Ref` and `pointer_to_objref`.

Let’s say I have a function that is like so

```julia
mutable struct UserData
    x::Int
end

function foo(bar, userdata, err)
    ccall((:foo, liblibrary), Cvoid, (Ptr{Cvoid},), userdata)
end

```

Is there any difference between the following code? If so, can someone elaborate on what the difference is?

```julia
userdata = UserData(1)
r = Ref(userdata)
x = foo(r)

```

And

```julia
userdata = UserData(1)
p = pointer_to_objref(userdata)
x = foo(p)

```

The documentation says

> For C code accepting pointers, [`Ref{T}`](https://docs.julialang.org/en/v1/base/c/#Core.Ref) should generally be used for the types of input arguments, allowing the use of pointers to memory managed by either Julia or C through the implicit call to [`Base.cconvert`](https://docs.julialang.org/en/v1/base/c/#Base.cconvert).

[https://docs.julialang.org/en/v1/manual/calling-c-and-fortran-code/#When-to-use-T,-Ptr{T}-and-Ref{T}-1](https://docs.julialang.org/en/v1/manual/calling-c-and-fortran-code/#When-to-use-T,-Ptr%7BT%7D-and-Ref%7BT%7D-1)

Why does the documentation say “_generally_ be used for types of input arguments”? What would be exceptions to this general rule? In what situations would it be appropriate to call `pointer_to_objref` on an instance of a mutable Julia struct?

---

<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:** [March 4, 2020, 6:14pm UTC](https://discourse.julialang.org/t/clarification-on-the-differences-between-ptr-and-ref/35531/2 "2020-03-04T18:14:28Z")

</div>

> [@kdheepak](#):
>
> Is there any difference between the following code? If so, can someone elaborate on what the difference is?

Yes. The second version is garbage. (i.e. the value you get in C is not usable.)

> [@kdheepak](#):
>
> Why does the documentation say “ _generally_ be used for types of input arguments”?

It’s talking about the argument type in the `ccall`, i.e. where you have `Ptr{Cvoid}` and it mosly apply to when you know the type you’ll use so that you can write `ccall(.... (Ref{Int}, ...), 1)`.

> [@kdheepak](#):
>
> In what situations would it be appropriate to call `pointer_to_objref` on an instance of a mutable Julia struct?

Almost never. In the context of `ccall` the function is there to implement `unsafe_convert`. Using it would be equivalent to using `unsafe_convert`. If you don’t have a case where you need `unsafe_convert`, don’t use `pointer_from_objref`.
