# C interface for void\*\*

**URL:** <https://discourse.julialang.org/t/c-interface-for-void/34221>\
**Category:** New to Julia\
**Created:** [February 5, 2020, 8:12pm UTC](https://discourse.julialang.org/t/c-interface-for-void/34221 "2020-02-05T20:12:12Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![RGerzaguet](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rgerzaguet/32/45492_2.png) [@RGerzaguet](https://discourse.julialang.org/u/RGerzaguet)\
**Post date:** [February 5, 2020, 8:12pm UTC](https://discourse.julialang.org/t/c-interface-for-void/34221/1 "2020-02-05T20:12:13Z")

</div>

Hello Guys,

A quite simple question on the C API and use with Julia  
I want to interface Julia functions with an existing C library cLib. This library has a function(let’s call it cfunction) that creates a buffer and fills it with Cfloat. The issue is that for some (opaque) reasons, they cast the buffer into a void\*\* before filling it.

In another words, the C lines are as follow

```
float* buff = malloc(N* sizeof(float));
void**ptr	= (void**)&buff;
cfunction(ptr); 

```

I want to directly interface Julia code with cfunction with ccall, so I try something like that

```
buff = Vector{Cfloat}(undef,N);		
ptr = Ref(buff): # The (void**)&buff, that does not do the trick :) 
ccall((:cfunction,cLib),Cvoid,(Ptr{Cvoid},),ptr); # Calling the function 

```

Aaaaaand it’s segfault. Any idea to address this ?  
Thanks in advance

---

<div class="post-metadata">

**Author:** ![mwolff](https://avatars.discourse-cdn.com/v4/letter/m/c2a13f/32.png) [@mwolff](https://discourse.julialang.org/u/mwolff)\
**Post date:** [February 5, 2020, 8:28pm UTC](https://discourse.julialang.org/t/c-interface-for-void/34221/2 "2020-02-05T20:28:02Z")

</div>

Without having tested but try it like this.

[https://docs.julialang.org/en/v1/manual/calling-c-and-fortran-code/#mapping-c-types-to-julia-1](https://docs.julialang.org/en/v1/manual/calling-c-and-fortran-code/#mapping-c-types-to-julia-1)

```julia
Ptr{Ptr{Cvoid}}

```

best regards  
Michael

---

<div class="post-metadata">

**Author:** ![RGerzaguet](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rgerzaguet/32/45492_2.png) [@RGerzaguet](https://discourse.julialang.org/u/RGerzaguet)\
**Post date:** [February 6, 2020, 8:42am UTC](https://discourse.julialang.org/t/c-interface-for-void/34221/3 "2020-02-06T08:42:47Z")

</div>

Many thank’s for the answer.

The issue if I try to use a Ptr{Ptr{Cvoid}} is that Julia try to convert it, I have to create a Base.unsafe\_convert method to pass from Base.RefValue{Array{Float32,1}} to Ptr{Ptr{Nothing}}

and I don’t know how to do this. I try innocently to create a method as

```
Base.unsafe_convert(T::Type{Ptr{Ptr{Cvoid}}}, t::Base.RefValue{Array{Float32,1}}) = t;

```

but of course it raise an error:

```
ERROR: TypeError: in ccall argument 2, expected Ptr{Ptr{Nothing}}, got Base.RefValue{Array{Float32,1}}

```

Any idea on how to create the convert method ?

---

<div class="post-metadata">

**Author:** ![mwolff](https://avatars.discourse-cdn.com/v4/letter/m/c2a13f/32.png) [@mwolff](https://discourse.julialang.org/u/mwolff)\
**Post date:** [February 6, 2020, 2:02pm UTC](https://discourse.julialang.org/t/c-interface-for-void/34221/4 "2020-02-06T14:02:21Z")

</div>

I have experimented a bit with your example using only integer values

```julia
arr=Int[10,20,30,40,50,60,70,80,90,100]
parr=Ref(arr)
ccall((:cFunction, LIB), Cvoid, (Ptr{Cvoid},Cint), Base.cconvert(Ptr{Ptr{Cvoid}},parr), 10)

```

my cFunction looks like this

```julia
void cFunction(void **ptr, int l){
    int**iPtr = (int**)ptr;
    for (int x=0; x < l; x++){
        printf("Wert : %d\n", *(*iPtr+x));
    }
}

```

best regards  
Michael

---

<div class="post-metadata">

**Author:** ![tomaklutfu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomaklutfu/32/2411_2.png) [@tomaklutfu](https://discourse.julialang.org/u/tomaklutfu)\
**Post date:** [February 6, 2020, 6:12pm UTC](https://discourse.julialang.org/t/c-interface-for-void/34221/5 "2020-02-06T18:12:50Z")

</div>

It seems that `ptr` should be

```julia
buff = Vector{Cfloat}(undef,N)	
ptr = Ref(Ptr{Cvoid}(pointer(buff)))
ccall((:cfunction,cLib),Cvoid,(Ptr{Ptr{Cvoid}},),ptr)

```

---

<div class="post-metadata">

**Author:** ![mwolff](https://avatars.discourse-cdn.com/v4/letter/m/c2a13f/32.png) [@mwolff](https://discourse.julialang.org/u/mwolff)\
**Post date:** [February 6, 2020, 7:48pm UTC](https://discourse.julialang.org/t/c-interface-for-void/34221/6 "2020-02-06T19:48:57Z")

</div>

Your example also works for me and looks much clearer 👍

best regards  
Michael

---

<div class="post-metadata">

**Author:** ![RGerzaguet](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rgerzaguet/32/45492_2.png) [@RGerzaguet](https://discourse.julialang.org/u/RGerzaguet)\
**Post date:** [February 7, 2020, 12:43pm UTC](https://discourse.julialang.org/t/c-interface-for-void/34221/7 "2020-02-07T12:43:13Z")

</div>

Hello Guys,

Many thanks for these quick answers, it works now 🙂
