# C null pointer to custom type

**URL:** <https://discourse.julialang.org/t/c-null-pointer-to-custom-type/8885>\
**Category:** General Usage\
**Tags:** ccall\
**Created:** [February 7, 2018, 8:13am UTC](https://discourse.julialang.org/t/c-null-pointer-to-custom-type/8885 "2018-02-07T08:13:39Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![mopg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mopg/32/3368_2.png) [@mopg](https://discourse.julialang.org/u/mopg)\
**Post date:** [February 7, 2018, 8:13am UTC](https://discourse.julialang.org/t/c-null-pointer-to-custom-type/8885/1 "2018-02-07T08:13:39Z")

</div>

I am coupling to a C library and would like to pass null pointers to a function. However, I want to pass a null pointer of type `Ptr{<mytype>}`. When I do that I end up with a convert error, because `C_NULL` of `Ptr{Void}`. It would be most straightforward if I could just use `C_NULL` everywhere.

Is there a way to write a custom convert function? I tried  
`convert(::Type{Ptr{<mytype>}}, C_NULL) = Ref{<mytype>}(0)`  
but that didn’t seem to do anything.

---

<div class="post-metadata">

**Author:** ![iblis17](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iblis17/32/649_2.png) [@iblis17](https://discourse.julialang.org/u/iblis17)\
**Post date:** [February 7, 2018, 9:56am UTC](https://discourse.julialang.org/t/c-null-pointer-to-custom-type/8885/2 "2018-02-07T09:56:00Z")

</div>

what is your actual `ccall`?

This looks good for me

```julia
> struct Foo end

> Ptr{Foo}(C_NULL)
Ptr{Foo} @0x0000000000000000

```

---

<div class="post-metadata">

**Author:** ![foobar\_lv2](https://avatars.discourse-cdn.com/v4/letter/f/ee59a6/32.png) [@foobar\_lv2](https://discourse.julialang.org/u/foobar_lv2)\
**Post date:** [February 7, 2018, 12:30pm UTC](https://discourse.julialang.org/t/c-null-pointer-to-custom-type/8885/3 "2018-02-07T12:30:56Z")

</div>

`reinterpret(Ptr{foo}, 0)` does the job for me. A null-pointer is after all an integer-zero.

---

<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 7, 2018, 12:41pm UTC](https://discourse.julialang.org/t/c-null-pointer-to-custom-type/8885/4 "2018-02-07T12:41:40Z")

</div>

There is no need for `reinterpret` here. The standard way to do it is just to use the constructor, as already shown, (which just bitcasts `C_NULL` to `Ptr{Foo}` but there is a reason why there exists higher-level interfaces).

---

<div class="post-metadata">

**Author:** ![mopg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mopg/32/3368_2.png) [@mopg](https://discourse.julialang.org/u/mopg)\
**Post date:** [February 7, 2018, 1:22pm UTC](https://discourse.julialang.org/t/c-null-pointer-to-custom-type/8885/5 "2018-02-07T13:22:28Z")

</div>

So an example would be for the function `foobar`:  
`ccall((:foobar, lib_include), Cint, (Ptr{foo}), C_NULL)`  
which does not work because `C_NULL` is of type `Ptr{Void}`.  
So is there a way to convert `C_NULL` automatically to a type of `Ptr{foo}`?

---

<div class="post-metadata">

**Author:** ![iblis17](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iblis17/32/649_2.png) [@iblis17](https://discourse.julialang.org/u/iblis17)\
**Post date:** [February 7, 2018, 1:56pm UTC](https://discourse.julialang.org/t/c-null-pointer-to-custom-type/8885/6 "2018-02-07T13:56:17Z")

</div>

Does `ccall((:foobar, lib_include), Cint, (Ptr{foo},), Ptr{foo}(C_NULL))` work for you?

---

<div class="post-metadata">

**Author:** ![mopg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mopg/32/3368_2.png) [@mopg](https://discourse.julialang.org/u/mopg)\
**Post date:** [February 7, 2018, 2:20pm UTC](https://discourse.julialang.org/t/c-null-pointer-to-custom-type/8885/7 "2018-02-07T14:20:48Z")

</div>

Yes, that works. But I was wondering if there was a way to do that automatically? In the same way a `C_NULL` can be used for a `Ptr{Cint}`?

---

<div class="post-metadata">

**Author:** ![iblis17](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iblis17/32/649_2.png) [@iblis17](https://discourse.julialang.org/u/iblis17)\
**Post date:** [February 7, 2018, 4:31pm UTC](https://discourse.julialang.org/t/c-null-pointer-to-custom-type/8885/8 "2018-02-07T16:31:45Z")

</div>

Perhaps

```julia
Base.cconvert(::Type{Ptr{Foo}}, ::Ptr{Void}) = Ptr{Foo}(C_NULL)

```

---

<div class="post-metadata">

**Author:** ![iblis17](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iblis17/32/649_2.png) [@iblis17](https://discourse.julialang.org/u/iblis17)\
**Post date:** [February 7, 2018, 4:34pm UTC](https://discourse.julialang.org/t/c-null-pointer-to-custom-type/8885/9 "2018-02-07T16:34:44Z")

</div>

But I do not suggest that changing the behaviour of builtin type like  
`Cint`,  
since that will affect other packages (even Base).
