# What is the correct way to create a "pointer to pointer"?

**URL:** <https://discourse.julialang.org/t/what-is-the-correct-way-to-create-a-pointer-to-pointer/71442>\
**Category:** General Usage\
**Tags:** question, ccall\
**Created:** [November 14, 2021, 1:01am UTC](https://discourse.julialang.org/t/what-is-the-correct-way-to-create-a-pointer-to-pointer/71442 "2021-11-14T01:01:09Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![heyx3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/heyx3/32/29811_2.png) [@heyx3](https://discourse.julialang.org/u/heyx3)\
**Post date:** [November 14, 2021, 1:01am UTC](https://discourse.julialang.org/t/what-is-the-correct-way-to-create-a-pointer-to-pointer/71442/1 "2021-11-14T01:01:09Z")

</div>

OpenGL has a function for compiling shader source code. It takes an array of strings, a.k.a. `const char**`. I can see an example in _ModernGL.jl_ of how to do this with pointers:

```julia
source::String = "..."
ptr_to_ptr = convert(Ptr{UInt8}, pointer([convert(Ptr{GLchar}, pointer(source))]))
glShaderSource(shader, 1, ptr_to_ptr, C_NULL)

```

However, my understanding is that `Ptr` is dangerous compared to the more modern `Ref`. The docs say that `Ref{Any}` is a special case that provides a reference to a reference, but in practice it doesn’t seem to work when I make the ccall with `Ref{Any}(source)`.

What is the best-practice way to pass a nested pointer into a ccall?

---

<div class="post-metadata">

**Author:** ![melonedo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/melonedo/32/15503_2.png) [@melonedo](https://discourse.julialang.org/u/melonedo)\
**Post date:** [November 14, 2021, 5:33am UTC](https://discourse.julialang.org/t/what-is-the-correct-way-to-create-a-pointer-to-pointer/71442/2 "2021-11-14T05:33:12Z")

</div>

For strings, just use `Ptr{Ptr{Cchar}}` in the ccall type signature, and the rest is done by Julia. You can refer to [https://docs.julialang.org/en/v1/manual/calling-c-and-fortran-code/#man-bits-types](https://docs.julialang.org/en/v1/manual/calling-c-and-fortran-code/#man-bits-types). As for the case in ModernGL.jl, simply passing a vector of strings is fine.

I have created a [demo](https://github.com/melonedo/pointer-to-pointer-demo) for this, feel free if you have any questions!

> [@heyx3](#):
>
> The docs say that `Ref{Any}` is a special case that provides a reference to a reference, but in practice it doesn’t seem to work when I make the ccall with `Ref{Any}(source)` .

`Any` is a Julia specific container that is capable of holding any Julia value, just like `void *` in C. Generally, this is used to pass extra arguments to C functions, which will eventually be used by a Julia function. [This blog post](https://julialang.org/blog/2013/05/callback/) demonstrates the typical use case. As Julia objects are far more complicated than simple pointers, this is not equivalent to a reference or pointer.

---

<div class="post-metadata">

**Author:** ![carstenbauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carstenbauer/32/4981_2.png) [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)\
**Post date:** [November 14, 2021, 8:24am UTC](https://discourse.julialang.org/t/what-is-the-correct-way-to-create-a-pointer-to-pointer/71442/3 "2021-11-14T08:24:18Z")

</div>

FWIW, in [LIKWID.jl](https://github.com/JuliaPerf/LIKWID.jl) I had to wrap a C API and in a few cases, a reference to a `char**` was required as input on the C side (to realize multiple string outputs). [In the `ccall`](https://github.com/JuliaPerf/LIKWID.jl/blob/054f00a35439f7a528c25efdcbd8da657ff8138d/src/LibLikwid.jl#L889) I have `Ptr{Ptr{Ptr{Cchar}}}` for the input argument and to call it, I create and pass over a `Ref{Ptr{Ptr{Cchar}}}()`. Later, I use `unsafe_wrap` to get the strings.
