# What type to use for 2D arrays in ccall

**URL:** <https://discourse.julialang.org/t/what-type-to-use-for-2d-arrays-in-ccall/97060>\
**Category:** General Usage\
**Tags:** ccall, array\
**Created:** [April 4, 2023, 10:58am UTC](https://discourse.julialang.org/t/what-type-to-use-for-2d-arrays-in-ccall/97060 "2023-04-04T10:58:52Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alseidon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alseidon/32/207775_2.png) [@Alseidon](https://discourse.julialang.org/u/Alseidon)\
**Post date:** [April 4, 2023, 10:58am UTC](https://discourse.julialang.org/t/what-type-to-use-for-2d-arrays-in-ccall/97060/1 "2023-04-04T10:58:52Z")

</div>

I am experimenting with calls to C functions, and got a bit puzzled when using multi-dimensional arrays as parameters.

I have this bit of C code in `cfile.c`:

```plaintext
void f(double array[2][2])
{
    array[1][1] = 42.;
    return;
}

```

which I compile with `gcc -fPIC -c cfile.c && gcc -shared -fPIC cfile.o -o cfile.so`. Then I import the function in Julia :

```julia
using Libdl

lib = Libdl.dlopen("./cfile.so")
func = Libdl.dlsym(lib, :f)

a = rand(Cdouble, 2, 2)
ccall(func, Cvoid, (Ref{Ref{Cdouble}}, ), a)
println(a)

Libdl.dlclose(lib)

```

Now with this bit of code, `a` doesn’t change. However, if I use `(Ptr{Cvoid}, )` as parameter type in the `ccall`, it works as intended. And if I use `Ptr{Ptr{Cdouble}}`, I get an error:

```julia
ERROR: LoadError: MethodError: no method matching unsafe_convert(::Type{Ptr{Float64}}, ::Float64)

```

Is there something I don’t do right? Or should I simply use `Ptr{Cvoid}` with multi-dimensional arrays?

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [April 4, 2023, 11:49am UTC](https://discourse.julialang.org/t/what-type-to-use-for-2d-arrays-in-ccall/97060/2 "2023-04-04T11:49:14Z")

</div>

I _believe_ that `Ptr{Cdouble}` should be fine. Passing multidimensional arrays around in C is notoriously difficult to get right and unless you are an expert C programmer I would advise against using them at all. Cf.

- [c - How are multi-dimensional arrays formatted in memory? - Stack Overflow](https://stackoverflow.com/questions/2565039/how-are-multi-dimensional-arrays-formatted-in-memory),
- [Question 6.18](https://c-faq.com/aryptr/pass2dary.html),
- and the rest of [Arrays and Pointers](https://c-faq.com/aryptr/).

---

<div class="post-metadata">

**Author:** ![Alseidon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alseidon/32/207775_2.png) [@Alseidon](https://discourse.julialang.org/u/Alseidon)\
**Post date:** [April 4, 2023, 12:09pm UTC](https://discourse.julialang.org/t/what-type-to-use-for-2d-arrays-in-ccall/97060/3 "2023-04-04T12:09:11Z")

</div>

This is working indeed! Although I still don’t get what’s happening with the other declarations, I will use this. Unfortunately, I have to use 2D arrays (in the real use case, I am calling a library function which modifies a 2D array passed as parameter). I could wrap the function to flatten them though… Is this what would be the best?
