# C Interface -- How to store an element in a C array

**URL:** <https://discourse.julialang.org/t/c-interface-how-to-store-an-element-in-a-c-array/22156>\
**Category:** General Usage\
**Created:** [March 21, 2019, 5:22pm UTC](https://discourse.julialang.org/t/c-interface-how-to-store-an-element-in-a-c-array/22156 "2019-03-21T17:22:27Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![danielc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danielc/32/220_2.png) [@danielc](https://discourse.julialang.org/u/danielc)\
**Post date:** [March 21, 2019, 5:22pm UTC](https://discourse.julialang.org/t/c-interface-how-to-store-an-element-in-a-c-array/22156/1 "2019-03-21T17:22:28Z")

</div>

Hello,

I have an an old bit of code, written by someone else, that I need to convert to Julia 1.0. I’ve begun the conversion, but I’m stuck:

```julia
struct ttvfast_RV_entry
  time::Cdouble
  RV::Cdouble
end
...
nrvs = 100
rv_workspace = Libc.calloc(nrvs,sizeof(ttvfast_RV_entry))

# ... rv_times is a regular Julia Array{Float,1}.
for i in 1:nrvs
    pointer_to_array(
        convert(Ptr{ttvfast_RV_entry},rv_workspace),
        nrvs
    )[i] = ttvfast_RV_entry(rv_times[i],0.0)
end

```

The function `pointer_to_array` no longer exists in Julia. I’m not 100% sure of what it did, but clearly the goal is to populate the C array `rv_workspace`.

I’ve been reading through the [documentation](https://docs.julialang.org/en/v1/manual/calling-c-and-fortran-code/), but I can’t figure out how to insert a value into a C array; especially for a non-primitive type. I would welcome any suggestions. I’m sure there’s a straight forward way to do this.

Thanks!

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [March 21, 2019, 5:43pm UTC](https://discourse.julialang.org/t/c-interface-how-to-store-an-element-in-a-c-array/22156/2 "2019-03-21T17:43:34Z")

</div>

Just as a general piece of advice, you will have a _much_ easier time upgrading code if you take the time to go through every Julia version, rather than skipping all the way to 1.0. If the code was written for Julia v0.5, then I would suggest:

- Test with 0.5
- Test with 0.6 and fix any deprecation warnings
- Test with 0.7 and fix any deprecation warnings
- Test with 1.0

That way you’ll get precise deprecation messages that tell you how to change the code, rather than just having to guess what functions that no longer exist used to do.

See also [PSA: use Julia 0.7 if you are upgrading](https://discourse.julialang.org/t/psa-use-julia-0-7-if-you-are-upgrading/13321)

---

<div class="post-metadata">

**Author:** ![Elrod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elrod/32/22461_2.png) [@Elrod](https://discourse.julialang.org/u/Elrod)\
**Post date:** [March 21, 2019, 5:49pm UTC](https://discourse.julialang.org/t/c-interface-how-to-store-an-element-in-a-c-array/22156/3 "2019-03-21T17:49:17Z")

</div>

Is this just a simple example:

```julia
rv_workspace = Libc.calloc(nrvs,sizeof(ttvfast_RV_entry))
...
    pointer_to_array(
        convert(Ptr{ttvfast_RV_entry},rv_workspace),
        nrvs
    )

```

Or are you actually allocating the memory in Julia?  
If so, just create it as a regular Julia array in the first place, and call `pointer` on it if you need a pointer (although `ccall` does that automatically).

If you’re allocating memory from the `C` side, you can use `unsafe_wrap`.

---

<div class="post-metadata">

**Author:** ![danielc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danielc/32/220_2.png) [@danielc](https://discourse.julialang.org/u/danielc)\
**Post date:** [March 21, 2019, 7:08pm UTC](https://discourse.julialang.org/t/c-interface-how-to-store-an-element-in-a-c-array/22156/4 "2019-03-21T19:08:39Z")

</div>

@Elrod That is the actual code. It’s not mine but I’m sure they’d accept a PR from me. Yes, it’s actually allocating the memory in Julia. Thanks for the advice! I’ll try to change it to a regular Julia array and call `pointer`.

---

<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:** [March 22, 2019, 12:52am UTC](https://discourse.julialang.org/t/c-interface-how-to-store-an-element-in-a-c-array/22156/5 "2019-03-22T00:52:37Z")

</div>

> [@danielc](#):
>
> pointer\_to\_array( convert(Ptr{ttvfast\_RV\_entry},rv\_workspace), nrvs )[i] = ttvfast\_RV\_entry(rv\_times[i],0.0)

Use `unsafe_store!` instead on the pointer. (Feel free to comment for any usage questions after reading the docs for it.)
