# Pointer to Julia function in ccall

**URL:** https://discourse.julialang.org/t/pointer-to-julia-function-in-ccall/3768
**Category:** General Usage
**Created:** [May 17, 2017, 9:51am UTC](https://discourse.julialang.org/t/pointer-to-julia-function-in-ccall/3768 "2017-05-17T09:51:29Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![igor.cerovsky](https://avatars.discourse-cdn.com/v4/letter/i/c37758/32.png) [@igor.cerovsky](https://discourse.julialang.org/u/igor.cerovsky)
#### Post date: [May 17, 2017, 9:51am UTC](https://discourse.julialang.org/t/pointer-to-julia-function-in-ccall/3768/1 "2017-05-17T09:51:29Z")

</div>

Is there a way to use pointer to Julia function in ccall? Following the documentation [https://docs.julialang.org/en/stable/manual/calling-c-and-fortran-code/](https://docs.julialang.org/en/stable/manual/calling-c-and-fortran-code/) the kernel (Julia 0.5.1) crashes (and there is no alias for C `void` in Julia). Obviously, the example from documentation works.

```julia
# Julia C-like function void(__stdcall*)(float progress);
function clbProgress(f::Float32)
  print(f)  
  return Void
end

function progress(fnc::Ptr{Void}, progress::Float32)
  fnc != Ptr{Void}(0) && ccall(fnc, Void, (Float32, ), progress)
end

const fnc_c = cfunction(clbProgress, Void, (Ref{Cfloat})) # here the 0.5.1 kernel crashes
progress(fnc_c, 0.1f0)

```

Note: using a C-imported function pointer works.

---

<div class="post-metadata">

### Author: ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)
#### Post date: [May 17, 2017, 10:55am UTC](https://discourse.julialang.org/t/pointer-to-julia-function-in-ccall/3768/2 "2017-05-17T10:55:40Z")

</div>

The crash is due to the fact that the list of input types should be a tuple and a one-element tuple is indicated by `(x,)`. In addition, you have to indicate the type of the return value, which is `Type{Void}`, not `Void` itself:

```julia
function clbProgress(f::Float32)
  print(f)
  return Void
end

function progress(fnc::Ptr{Void}, progress::Float32)
  fnc != C_NULL && ccall(fnc, Type{Void}, (Float32, ), progress)
end

const fnc_c = cfunction(clbProgress, Type{Void}, (Ref{Cfloat},))
progress(fnc_c, 0.1f0)

```

---

<div class="post-metadata">

### Author: ![igor.cerovsky](https://avatars.discourse-cdn.com/v4/letter/i/c37758/32.png) [@igor.cerovsky](https://discourse.julialang.org/u/igor.cerovsky)
#### Post date: [May 17, 2017, 1:52pm UTC](https://discourse.julialang.org/t/pointer-to-julia-function-in-ccall/3768/3 "2017-05-17T13:52:24Z")

</div>

thanks for the correcting the code, now it compiles fine, but it doesn’t give correct results… any idea?

```julia
julia> progress(fnc_c, 0.1f0)
-8.103731f22
Void

julia> p = 1.1f0
1.1f0

julia> ccall(fnc_c, Type{Void}, (Float32, ), p)
0.0
Void

# just check here
julia> clbProgress(2.2f0)
2.2
Void

```

---

<div class="post-metadata">

### Author: ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)
#### Post date: [May 17, 2017, 1:55pm UTC](https://discourse.julialang.org/t/pointer-to-julia-function-in-ccall/3768/4 "2017-05-17T13:55:34Z")

</div>

> [@igor.cerovsky](#):
>
> any idea?

Yes, coherently use either `Cfloat` or `Ref{Cfloat}` as input type:

```julia
function clbProgress(f::Float32)
  print(f)
  return Void
end

function progress(fnc::Ptr{Void}, progress::Float32)
  fnc != Ptr{Void}(0) && ccall(fnc, Type{Void}, (Cfloat, ), progress)
end

const fnc_c = cfunction(clbProgress, Type{Void}, (Cfloat,))
progress(fnc_c, 0.1f0)

```

I don’t know why you wanted to use a `Ref` before, so here I went with `Cfloat`.

---

<div class="post-metadata">

### Author: ![igor.cerovsky](https://avatars.discourse-cdn.com/v4/letter/i/c37758/32.png) [@igor.cerovsky](https://discourse.julialang.org/u/igor.cerovsky)
#### Post date: [May 17, 2017, 2:01pm UTC](https://discourse.julialang.org/t/pointer-to-julia-function-in-ccall/3768/5 "2017-05-17T14:01:43Z")

</div>

thanks again, good point! I took the `Ref` from docs; and yes it is a pointer to the value, not the value itself i want to pass!
