# Reading bytes from a pointer passed to C via ccall in Julia

**URL:** <https://discourse.julialang.org/t/reading-bytes-from-a-pointer-passed-to-c-via-ccall-in-julia/127014>\
**Category:** Specific Domains\
**Tags:** question, ccall\
**Created:** [March 16, 2025, 4:26pm UTC](https://discourse.julialang.org/t/reading-bytes-from-a-pointer-passed-to-c-via-ccall-in-julia/127014 "2025-03-16T16:26:52Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ochibobo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ochibobo/32/18765_2.png) [@Ochibobo](https://discourse.julialang.org/u/Ochibobo)\
**Post date:** [March 16, 2025, 4:26pm UTC](https://discourse.julialang.org/t/reading-bytes-from-a-pointer-passed-to-c-via-ccall-in-julia/127014/1 "2025-03-16T16:26:52Z")

</div>

Hello…  
I have a C function that has a pointer as one of the parameters. The function structure looks as follows:

```julia
int fn(..., void** result){ // body... *result = some_bytes; // body... }

```

In Julia, part of the `ccall` parameter is `Ptr{Ptr{Void}}` that matches the `result` argument in the function above.  
This `result` argument is actually modified by the `C` funtion.To represent the `result` argument in `Julia`, I use a variable, say `julia_result = Ref{Ptr{Void}}(C_NULL)`. I’ve also trued removing the `C_NULL` there. After executing the `ccall` , all other parameters seem to work as expected but `unsafe_load(julia_result[])` always returns nothing. Given the output of the function, I would expect that the `julia_result` variable would also contain some data.What would be the appropriate way to pass this variable in order to read the value written onto it? Thanks

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [March 16, 2025, 4:56pm UTC](https://discourse.julialang.org/t/reading-bytes-from-a-pointer-passed-to-c-via-ccall-in-julia/127014/2 "2025-03-16T16:56:41Z")

</div>

> [@Ochibobo](#):
>
> `unsafe_load(julia_result[])` always returns nothing

That’s because it’s a point to `void`. What is the _actual_ type and size of the result being allocated?

---

<div class="post-metadata">

**Author:** ![Ochibobo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ochibobo/32/18765_2.png) [@Ochibobo](https://discourse.julialang.org/u/Ochibobo)\
**Post date:** [March 16, 2025, 5:21pm UTC](https://discourse.julialang.org/t/reading-bytes-from-a-pointer-passed-to-c-via-ccall-in-julia/127014/4 "2025-03-16T17:21:25Z")

</div>

I’m actually writing binary data/a bytes to the `result`. There’s another variable that tracks the allocated size and it returns it to the Julia function; this seems to work.

Essentially, the assigned bytes are a serialized proto.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [March 16, 2025, 8:48pm UTC](https://discourse.julialang.org/t/reading-bytes-from-a-pointer-passed-to-c-via-ccall-in-julia/127014/5 "2025-03-16T20:48:02Z")

</div>

> [@Ochibobo](#):
>
> I’m actually writing binary data/a bytes to the `result`. There’s another variable that tracks the allocated size and it returns it to the Julia function

So what you want in Julia is an array of `UInt8`, and you know the length `len`? Then the easiest thing to do is:

```julia
a = unsafe_wrap(Ptr{UInt8}(julia_result[]), len)

```

to get a Julia `Vector{UInt8}` that you can access normally. (However, you should be careful about who is in charge of de-allocating the array, and when it will be freed.)

Alternatively, you can convert the pointer to `Ptr{UInt8}` and just use `unsafe_load` to access individual bytes.
