# Jl\_ptr\_to\_array

**URL:** <https://discourse.julialang.org/t/jl-ptr-to-array/4436>\
**Category:** General Usage\
**Created:** [June 24, 2017, 2:32am UTC](https://discourse.julialang.org/t/jl-ptr-to-array/4436 "2017-06-24T02:32:30Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![tastelessjolt](https://avatars.discourse-cdn.com/v4/letter/t/f14d63/32.png) [@tastelessjolt](https://discourse.julialang.org/u/tastelessjolt)\
**Post date:** [June 24, 2017, 2:32am UTC](https://discourse.julialang.org/t/jl-ptr-to-array/4436/1 "2017-06-24T02:32:30Z")

</div>

How should argument `data` to the function `jl_ptr_to_array` be given? I know that in the case of `Array{Float64,1}` it’s just a pointer to double array.  
But suppose in the case of `Array{Complex{Float64},1}`, what should I pass as `data`? I tried `double complex **`, it didn’t work.  
And also in case of `Array{String,1}`, I tried passing `char **`. Even that didn’t work.

Thank you.

---

<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:** [June 24, 2017, 4:43am UTC](https://discourse.julialang.org/t/jl-ptr-to-array/4436/2 "2017-06-24T04:43:07Z")

</div>

Warning: I am _definitely_ not an expert, and haven’t used the embedding API. I probably have just the right amount of knowledge to be dangerous…

But, for what it’s worth, I’m pretty sure that a `Complex{Float64}` is just laid out in memory as two adjacent `Float64`s. That should be the same layout as a C struct containing two `double` elements (a `complex`, I guess?). As evidence of that:

```julia
julia> sizeof(Float64)
8

julia> sizeof(Complex{Float64})
16

```

So the data stored in a Julia `Array{Complex{Float64}, 1}` should, I think, just be a `complex *` in C.

---

<div class="post-metadata">

**Author:** ![ScottPJones](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/scottpjones/32/146_2.png) [@ScottPJones](https://discourse.julialang.org/u/ScottPJones)\
**Post date:** [June 24, 2017, 5:54pm UTC](https://discourse.julialang.org/t/jl-ptr-to-array/4436/3 "2017-06-24T17:54:15Z")

</div>

To construct a `Vector{String}`, I think you’d need to allocate a `Vector{String}` first of the right size, and then perform either `jl_cstr_to_string` or `jl_pchar_to_string` (depending on whether it is nul byte terminated or you have a length) on each element of the C/C++ vector, sticking those into the allocated vector.

---

<div class="post-metadata">

**Author:** ![tastelessjolt](https://avatars.discourse-cdn.com/v4/letter/t/f14d63/32.png) [@tastelessjolt](https://discourse.julialang.org/u/tastelessjolt)\
**Post date:** [June 25, 2017, 8:34pm UTC](https://discourse.julialang.org/t/jl-ptr-to-array/4436/4 "2017-06-25T20:34:49Z")

</div>

Thank you so much. This worked for Strings allocating first and then applying `jl_cstr_to_string` on every `String`. But for an `Array{Complex{Float64}}`, I tried first allocating the Array and then filling them with boxed `Complex{Float64}`, Julia couldn’t read the values correctly and it is always the same value.

---

<div class="post-metadata">

**Author:** ![ScottPJones](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/scottpjones/32/146_2.png) [@ScottPJones](https://discourse.julialang.org/u/ScottPJones)\
**Post date:** [June 25, 2017, 10:06pm UTC](https://discourse.julialang.org/t/jl-ptr-to-array/4436/5 "2017-06-25T22:06:11Z")

</div>

`String`, although conceptually an immutable, is implemented with a mutable type, and the vector has pointers in it.  
Do you have an `Array{Complex{Float64}}` (which doesn’t say how many dimensions it has), or more likely a `Vector` or `Matrix` of `Complex{Float64}`?  
`Complex{Float64}` is a `struct` (`immutable`) and gets allocated as two 64-bit IEEE floating point values, there is no boxing when they are placed in a `Array` (`Vector` or `Matrix` or n-d `Array`), you simply get  
[real part 1, imag part 1, real part 2, imag part 2, …] in memory.

---

<div class="post-metadata">

**Author:** ![tastelessjolt](https://avatars.discourse-cdn.com/v4/letter/t/f14d63/32.png) [@tastelessjolt](https://discourse.julialang.org/u/tastelessjolt)\
**Post date:** [June 26, 2017, 9:29pm UTC](https://discourse.julialang.org/t/jl-ptr-to-array/4436/6 "2017-06-26T21:29:35Z")

</div>

Thank you for replying. I just omitted the dimensions for simplicity, And I was able to create `Array{Complex{Float64}}`. Yeah, you’re right. I was just able to assign initialised `double complex` each position in the allocated vector.

Thank you.
