# Ccall: how to wrap call-by-reference C functions?

**URL:** <https://discourse.julialang.org/t/ccall-how-to-wrap-call-by-reference-c-functions/53510>\
**Category:** General Usage\
**Created:** [January 18, 2021, 1:14am UTC](https://discourse.julialang.org/t/ccall-how-to-wrap-call-by-reference-c-functions/53510 "2021-01-18T01:14:23Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![simon-anders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simon-anders/32/21063_2.png) [@simon-anders](https://discourse.julialang.org/u/simon-anders)\
**Post date:** [January 18, 2021, 1:14am UTC](https://discourse.julialang.org/t/ccall-how-to-wrap-call-by-reference-c-functions/53510/1 "2021-01-18T01:14:23Z")

</div>

Hi

I’m trying to wrap a C library, and I can’t figure out how references are handled in ccall.

Here is my study case: I have a simple C file, called `jlptrtest.c`, consisting of just this:

```julia
void put_7( int *i ) {
   *i = 7;
}

```

I’ve compiled it with `cc jlptrtest.c -fPIC -shared -o libptrtest.so` and am now trying to use it from Julia. I want to access the value 7. I’ve tried

```julia
a = 13
ccall( (:put_7, "libptrtest"), Cvoid, (Ref{Cint},), a )
println(a)

```

and it prints 13, not 7. Which type should `a` have, and how do I have to write the ccall so that I can get my 7?

Thanks in advance.

Simon

---

<div class="post-metadata">

**Author:** ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)\
**Post date:** [January 18, 2021, 1:33am UTC](https://discourse.julialang.org/t/ccall-how-to-wrap-call-by-reference-c-functions/53510/2 "2021-01-18T01:33:40Z")

</div>

> [@simon-anders](#):
>
> Which type should `a` have

The one you declare you’re passing to the C function, `Ref{Cint}`. Note that `Cint` is an alias for `Int32`, but integer literals on a 64-bit Julia are `Int64`

```julia
julia> a = Ref{Cint}(13)
Base.RefValue{Int32}(13)

julia> ccall((:put_7, "./libptrtest.so"), Cvoid, (Ref{Cint},), a)

julia> a
Base.RefValue{Int32}(7)

julia> a[]
7

```

For reference, see the relevant section of the manual: [Passing Pointers for Modifying Inputs](https://docs.julialang.org/en/v1/manual/calling-c-and-fortran-code/index.html#Passing-Pointers-for-Modifying-Inputs)

---

<div class="post-metadata">

**Author:** ![Gnimuc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gnimuc/32/2194_2.png) [@Gnimuc](https://discourse.julialang.org/u/Gnimuc)\
**Post date:** [January 18, 2021, 1:37am UTC](https://discourse.julialang.org/t/ccall-how-to-wrap-call-by-reference-c-functions/53510/3 "2021-01-18T01:37:32Z")

</div>

`ccall( (:put_7, "libptrtest"), Cvoid, (Ref{Cint},), a )`

What happened here was

1. `a` was `Base.cconvert`-ed to a tmp\_var of type `Ref{Cint}`
2. the `put_7` function in your c library edited the value of tmp\_var
3. `a` is left untouched

The fix is to define `a` as a Ref: `a_ref = Ref{Cint}(13)`

---

<div class="post-metadata">

**Author:** ![simon-anders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simon-anders/32/21063_2.png) [@simon-anders](https://discourse.julialang.org/u/simon-anders)\
**Post date:** [January 18, 2021, 1:39am UTC](https://discourse.julialang.org/t/ccall-how-to-wrap-call-by-reference-c-functions/53510/4 "2021-01-18T01:39:36Z")

</div>

Thanks a lot to both of you. I though I had tried that, too, but obviously I hadn’t. Now it works.
