# Initialize buffer for ccall

**URL:** https://discourse.julialang.org/t/initialize-buffer-for-ccall/52659
**Category:** New to Julia
**Created:** [December 31, 2020, 1:50pm UTC](https://discourse.julialang.org/t/initialize-buffer-for-ccall/52659 "2020-12-31T13:50:15Z")
**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: [December 31, 2020, 2:23pm UTC](https://discourse.julialang.org/t/initialize-buffer-for-ccall/52659/2 "2020-12-31T14:23:29Z")

</div>

`buffer = Ref{Array{Float64,1}}` is certainly wrong. This a type, not even an instance of a `Ref` type — that is, `buffer isa Type` returns `true`. From the documentation, `buffer` should be an _allocated_ array, i.e. `buffer` should be an `Array{Float64,1}` (a `Vector{Float64}`) of length `buffer_size_samples`.

Also, when you pass the `buffer` to the `ccall` it should be as a `Ptr{Cdouble}`. Julia knows how to pass an `Array` to a pointer argument. Note also that `ccall` will automatically convert arguments by calling `convert` — there’s no need to explicitly convert to `UInt8` etcetera.

That is, it should probably look something like this:

```julia
function mcc172_a_in_scan_read(address::Integer, samples_per_channel::Integer, mcc172_num_channels::Integer, timeout::Real)
	status = Ref{UInt16}()
	buffer_size_samples = samples_per_channel * mcc172_num_channels
	buffer = Vector{Float64}(undef, buffer_size_samples)
	samples_read_per_channel = Ref{UInt32}()
	
	success = ccall((:mcc172_a_in_scan_read, "/usr/local/lib/libdaqhats.so"),
	    Cint, (UInt8, Ref{UInt16}, UInt32, Cdouble, Ptr{Cdouble}, UInt32, Ref{UInt32}), 
	    address, status, samples_per_channel, timeout, buffer, buffer_size_samples, samples_read_per_channel)
    return buffer, Int(samples_read_per_channel[]), status[]
end

```

or maybe `resize!(buffer, samples_read_per_channel[] * mcc172_num_channels)`. Note that you don’t need to initialize the `Ref` arguments with a value since (if I understand the documentation for `mcc172_a_in_scan_read` correctly) they are purely output parameters.

---

_[View the full topic](https://discourse.julialang.org/t/initialize-buffer-for-ccall/52659)._
