# How to pass a reference to a vector to C

**URL:** <https://discourse.julialang.org/t/how-to-pass-a-reference-to-a-vector-to-c/136320>\
**Category:** General Usage\
**Tags:** ccall\
**Created:** [March 21, 2026, 1:02pm UTC](https://discourse.julialang.org/t/how-to-pass-a-reference-to-a-vector-to-c/136320 "2026-03-21T13:02:28Z")\
**Posts on this page:** 1\
**Showing post:** 2

<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 21, 2026, 2:09pm UTC](https://discourse.julialang.org/t/how-to-pass-a-reference-to-a-vector-to-c/136320/2 "2026-03-21T14:09:14Z")

</div>

> [@AwesomeQuest](#):
>
> I’m calling SimpleBLE which has an API where both the reading and writing functions take `(uint8_t** data, size_t* data_length)` that are used to return data in the read case and pass data in the write case.
> 
> My confusion lies in how one properly prepares a reference to a Julia vector to be passed to C.

Usually in an API like that the C function will be allocating the array, so you shouldn’t be passing a reference to an existing Julia vector. Instead, you would do something like:

```julia-auto
data_ptr = Ref{Ptr{UInt8}}()
data_length = Ref{Csize_t}()
@ccall somelibrary.somefunction(data_ptr::Ref{Ptr{UInt8}}, data_length::Ref{Csize_t})::Cvoid # if it returns void
data = unsafe_wrap(Array, data_ptr[], data_length[], owns=true)

```

where passing `owns=true` assumes that Julia is supposed to call `free` on the resulting array when it is done with it (otherwise you could make a copy, as in your other thread: [Does `String(unsafe_wrap(cstring, len, own=true))` free the memory it points to?](https://discourse.julialang.org/t/does-string-unsafe-wrap-cstring-len-own-true-free-the-memory-it-points-to/136315))

---

_[View the full topic](https://discourse.julialang.org/t/how-to-pass-a-reference-to-a-vector-to-c/136320)._
