# Ccall and cconvert for Ptr{T}

**URL:** <https://discourse.julialang.org/t/ccall-and-cconvert-for-ptr-t/1874>\
**Category:** General Usage\
**Created:** [February 3, 2017, 4:35pm UTC](https://discourse.julialang.org/t/ccall-and-cconvert-for-ptr-t/1874 "2017-02-03T16:35:56Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![thofma](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thofma/32/1691_2.png) [@thofma](https://discourse.julialang.org/u/thofma)\
**Post date:** [February 3, 2017, 4:35pm UTC](https://discourse.julialang.org/t/ccall-and-cconvert-for-ptr-t/1874/1 "2017-02-03T16:35:56Z")

</div>

Just a quick question about `ccall` and `cconvert`. From the manual I know that

```julia
ccall((:foo, "libfoo"), Void, (A, ), x)

```

will result in a call to

```julia
ccall((:foo, "libfoo"), Void, (A, ), unsafe_convert(A, cconvert(A, x)))

```

But what about the following:

```julia
ccall((:foo, "libfoo"), Void, (Ptr{A}, ), x)

```

Will this result in a call to

```julia
ccall((:foo, "libfoo"), Void, (Ptr{A}, ), unsafe_convert(Ptr{A}, cconvert(A,x)))

```

or is there something else going on?

Here is how this came up. I have types `A` and `B` for which I define `convert(Type{A}, ::B)`. Because of a typo I wrote

```julia
x::B
ccall((:foo, "libfoo"), Void, (Ptr{A}, ), x)

```

and it did not throw an error.

---

<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 3, 2017, 5:00pm UTC](https://discourse.julialang.org/t/ccall-and-cconvert-for-ptr-t/1874/2 "2017-02-03T17:00:04Z")

</div>

> [@thofma](#):
>
> ccall((:foo, “libfoo”), Void, (Ptr{A}, ), unsafe\_convert(Ptr{A}, cconvert(A,x)))

No, it’ll be

```julia
ccall((:foo, "libfoo"), Void, (Ptr{A}, ), unsafe_convert(Ptr{A}, cconvert(Ptr{A},x)))

```

There’s nothing special about `Ptr`.

---

<div class="post-metadata">

**Author:** ![thofma](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thofma/32/1691_2.png) [@thofma](https://discourse.julialang.org/u/thofma)\
**Post date:** [February 3, 2017, 5:28pm UTC](https://discourse.julialang.org/t/ccall-and-cconvert-for-ptr-t/1874/4 "2017-02-03T17:28:30Z")

</div>

Sorry, I think I wrote something wrong. I defined `convert(Type{A}, ::B)` and then the following worked

```julia
x::B
ccall((:foo, "libfoo"), Void, (Ptr{A}, ), &x)

```

It does not throw a method error. This is what surprised me.

---

<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 3, 2017, 5:49pm UTC](https://discourse.julialang.org/t/ccall-and-cconvert-for-ptr-t/1874/5 "2017-02-03T17:49:11Z")

</div>

That lowers to a different set of functions that are not documented since it’ll be deprecated. You can figure out all these by looking at `code_lowered` of the `ccall` and use `code_typed` to see which methods are called.

---

<div class="post-metadata">

**Author:** ![thofma](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thofma/32/1691_2.png) [@thofma](https://discourse.julialang.org/u/thofma)\
**Post date:** [February 3, 2017, 6:00pm UTC](https://discourse.julialang.org/t/ccall-and-cconvert-for-ptr-t/1874/6 "2017-02-03T18:00:45Z")

</div>

Thanks a lot and sorry for the confusion. Is there a roadmap for deprecating `&` in `ccall`? This has been called “deprecated” since 0.4, but I have not seen a deprecation warning so far. It is also used extensively in `Base.MPFR` and `Base.GMP`.

---

<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 3, 2017, 6:14pm UTC](https://discourse.julialang.org/t/ccall-and-cconvert-for-ptr-t/1874/7 "2017-02-03T18:14:25Z")

</div>

The function is fully covered by the `Ref` equivalent but we need better optimizations in type inference to make them perform the same. There isn’t currently a timeline for that. (It’s high, though not highest, on my todo list and I can’t say for others).
