# How to get a pointer Ptr{T} that references a variable of type T

**URL:** https://discourse.julialang.org/t/how-to-get-a-pointer-ptr-t-that-references-a-variable-of-type-t/9273
**Category:** General Usage
**Tags:** ccall
**Created:** [February 23, 2018, 2:16am UTC](https://discourse.julialang.org/t/how-to-get-a-pointer-ptr-t-that-references-a-variable-of-type-t/9273 "2018-02-23T02:16:27Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![tk3369](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tk3369/32/2824_2.png) [@tk3369](https://discourse.julialang.org/u/tk3369)
#### Post date: [February 23, 2018, 2:16am UTC](https://discourse.julialang.org/t/how-to-get-a-pointer-ptr-t-that-references-a-variable-of-type-t/9273/1 "2018-02-23T02:16:27Z")

</div>

I need to pass a `Ptr{T}` to a C function but `pointer_from_objref` returns `Ptr{Void}` rather than `Ptr{T}`… How should I do this?

In the following example, I need to pass a value of type `Ptr{Foo}` to the C function:

```julia
julia> struct Foo x end

julia> f = Foo(1)
Foo(1)

julia> pointer_from_objref(f)
Ptr{Void} @0x00007feff06d2ab0

```

---

<div class="post-metadata">

### Author: ![tk3369](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tk3369/32/2824_2.png) [@tk3369](https://discourse.julialang.org/u/tk3369)
#### Post date: [February 23, 2018, 2:47am UTC](https://discourse.julialang.org/t/how-to-get-a-pointer-ptr-t-that-references-a-variable-of-type-t/9273/2 "2018-02-23T02:47:37Z")

</div>

I found a solution… but is it the best way?

```julia
julia> convert(Ptr{Foo}, pointer_from_objref(f))
Ptr{Foo} @0x00007f2c94e27ad0

```

---

<div class="post-metadata">

### Author: ![Elrod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elrod/32/22461_2.png) [@Elrod](https://discourse.julialang.org/u/Elrod)
#### Post date: [February 23, 2018, 5:17am UTC](https://discourse.julialang.org/t/how-to-get-a-pointer-ptr-t-that-references-a-variable-of-type-t/9273/3 "2018-02-23T05:17:56Z")

</div>

If I’m not mistaken, you can also just tell ccall that the argument type is a `Ptr{Void}`.

---

<div class="post-metadata">

### Author: ![tk3369](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tk3369/32/2824_2.png) [@tk3369](https://discourse.julialang.org/u/tk3369)
#### Post date: [February 24, 2018, 2:26am UTC](https://discourse.julialang.org/t/how-to-get-a-pointer-ptr-t-that-references-a-variable-of-type-t/9273/4 "2018-02-24T02:26:37Z")

</div>

Yes, `Ptr{Void}` works. Thanks

---

<div class="post-metadata">

### Author: ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)
#### Post date: [February 24, 2018, 8:17am UTC](https://discourse.julialang.org/t/how-to-get-a-pointer-ptr-t-that-references-a-variable-of-type-t/9273/5 "2018-02-24T08:17:08Z")

</div>

`pointer_from_objref` is not right and is in fact removed for immutable objects in 0.7. Just wrap it in a `Ref`.

---

<div class="post-metadata">

### Author: ![tk3369](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tk3369/32/2824_2.png) [@tk3369](https://discourse.julialang.org/u/tk3369)
#### Post date: [February 24, 2018, 8:30pm UTC](https://discourse.julialang.org/t/how-to-get-a-pointer-ptr-t-that-references-a-variable-of-type-t/9273/6 "2018-02-24T20:30:31Z")

</div>

`Ref` doesn’t really work for me. I have over a thousand `ccall` functions generated from Clang. The method signatures contain arguments that take pointers as well as returning results as pointers. I have demonstrated the problem with a MWE [here](https://github.com/tk3369/julia-notebooks/blob/master/ccall%20-%20pointers%20to%20structures.ipynb).

Basically, the wrapper functions may accept `Ptr{T}` arguments and so I will need to call it with a pointer to the object. The `pointer_from_objref` function works very well except that it loses its type. The `Ref` function works only if it’s used inside the wrapper function, for which I would rather not do as I would have a thousand calls to edit (or mess with Clang, which would not be a very pleasant exercise.)

If I call the wrapper function with `Ref(x)`, it could not dispatch to the wrapper function since it expects `Ptr{T}`. If I change the method signature to accept a `RefValue{T})` type, then method dispatch works but the `ccall` crashes julia.

---

<div class="post-metadata">

### Author: ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)
#### Post date: [February 24, 2018, 8:34pm UTC](https://discourse.julialang.org/t/how-to-get-a-pointer-ptr-t-that-references-a-variable-of-type-t/9273/7 "2018-02-24T20:34:01Z")

</div>

> [@tk3369](#):
>
> The pointer\_from\_objref function works very well except that it loses its type.

And that is it completely unsafe and can crash at any point when you put those `ccalls` inside a function (and is hence an error on julia 0.7). See [Dissallow pointer\_from\_objref on immutable values · Issue #15857 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/15857).

---

<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: [February 24, 2018, 9:09pm UTC](https://discourse.julialang.org/t/how-to-get-a-pointer-ptr-t-that-references-a-variable-of-type-t/9273/8 "2018-02-24T21:09:53Z")

</div>

> [@tk3369](#):
>
> I have over a thousand ccall functions generated from Clang. The method signatures contain arguments that take pointers as well as returning results as pointers.

That was a Clang.jl bug [Generating safer wrapper for pointer arguments · Issue #152 · JuliaInterop/Clang.jl · GitHub](https://github.com/ihnorton/Clang.jl/issues/152). Please regenerate your wrapper.

---

<div class="post-metadata">

### Author: ![tk3369](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tk3369/32/2824_2.png) [@tk3369](https://discourse.julialang.org/u/tk3369)
#### Post date: [February 24, 2018, 11:23pm UTC](https://discourse.julialang.org/t/how-to-get-a-pointer-ptr-t-that-references-a-variable-of-type-t/9273/9 "2018-02-24T23:23:43Z")

</div>

That’s interesting! However, the latest version of Clang still generates bad signature for me. Apparently, it does not recognize a typedef that is a pointer to a struct.

Consider this simple function:

```nohighlight
typedef struct {
    double x;
} FOO, *FOOP;

FOOP test1(double a, double b, FOOP c);

```

It gets wrapped as

```julia
function test1(a::Cdouble, b::Cdouble, c::FOOP)
    ccall((:test1, test), FOOP, (Cdouble, Cdouble, FOOP), a, b, c)
end

```

But, it would be fine with `Foo *` instead:

```julia
typedef struct {
    double x;
} FOO, *FOOP;

FOO *test1(double a, double b, FOO *c);

```

turns into

```julia
function test1(a::Cdouble, b::Cdouble, c)
    ccall((:test1, test2), Ptr{FOO}, (Cdouble, Cdouble, Ptr{FOO}), a, b, c)
end

```

---

<div class="post-metadata">

### Author: ![tk3369](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tk3369/32/2824_2.png) [@tk3369](https://discourse.julialang.org/u/tk3369)
#### Post date: [February 25, 2018, 7:13am UTC](https://discourse.julialang.org/t/how-to-get-a-pointer-ptr-t-that-references-a-variable-of-type-t/9273/10 "2018-02-25T07:13:16Z")

</div>

I’ll submit an issue to Clang project for this similar bug.

Since the problem only happens at the signature, I can work around the issue by doing a global search and replace of `::T` with an empty string. So the end result isn’t too bad.

Thanks for your help.
