# Passing a list of arguments to a varargs C function

**URL:** <https://discourse.julialang.org/t/passing-a-list-of-arguments-to-a-varargs-c-function/5271>\
**Category:** General Usage\
**Tags:** question\
**Created:** [August 8, 2017, 3:38am UTC](https://discourse.julialang.org/t/passing-a-list-of-arguments-to-a-varargs-c-function/5271 "2017-08-08T03:38:11Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alexander\_Belopolsky](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alexander_belopolsky/32/4882_2.png) [@Alexander\_Belopolsky](https://discourse.julialang.org/u/Alexander_Belopolsky)\
**Post date:** [August 8, 2017, 3:38am UTC](https://discourse.julialang.org/t/passing-a-list-of-arguments-to-a-varargs-c-function/5271/1 "2017-08-08T03:38:11Z")

</div>

I can call a varargs C function like printf as follows

```julia
julia> ccall(:printf, Int32, (Cstring, Int32...), "%d %d\n", 42, 43)
42 43
6

```

However, if I try to pass a list of arguments, I get a strange error

```julia
julia> x = Int32[42, 43];

julia> ccall(:printf, Int32, (Cstring, Int32...), "%d %d\n", x...)
ERROR: MethodError: no method matching cconvert(::Type{Int32}, ::Int32, ::Int32)
Closest candidates are:
  cconvert(::Type, ::Any) at essentials.jl:149
  cconvert(::Type{Int32}, ::RawFD) at libc.jl:24
  cconvert(::Type{#s2} where #s2<:Ptr, ::Any) at essentials.jl:150
Stacktrace:
 [1] anonymous at ./<missing>:?

```

This looks like a ccall limitation, but I wonder whether there is a work-around.

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [August 8, 2017, 4:06am UTC](https://discourse.julialang.org/t/passing-a-list-of-arguments-to-a-varargs-c-function/5271/2 "2017-08-08T04:06:34Z")

</div>

> [@Alexander\_Belopolsky](#):
>
> whether there is a work-around.

No. Function call with unknown number of argument requires very special code generation. AFAICT LLVM does not support it. `libffi` should though you should be better off tweaking/picking a different C-API.

---

<div class="post-metadata">

**Author:** ![Alexander\_Belopolsky](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alexander_belopolsky/32/4882_2.png) [@Alexander\_Belopolsky](https://discourse.julialang.org/u/Alexander_Belopolsky)\
**Post date:** [August 14, 2017, 3:10am UTC](https://discourse.julialang.org/t/passing-a-list-of-arguments-to-a-varargs-c-function/5271/3 "2017-08-14T03:10:13Z")

</div>

> [@yuyichao](#):
>
> you should be better off tweaking/picking a different C-API.

Unfortunately, I don’t control the C API and for one function there is no alternative. However, the varargs are all of the same type and in practice only a small number is used. My work-around is to define a series of methods one for each number of varargs. This is a rather tedious repetition of lines like

```julia
f(x1::Ptr{T}) = ccall(:f, Int, (Ptr{T}, ) x1, C_NULL)
f(x1::Ptr{T}, x2::Ptr{T}) = ccall(:f, Int, (Ptr{T}, Ptr{T}), x1, x2, C_NULL)
...

```

and I wonder if I can replace that with some smart metaprogramming.
