# How to place an array as one of the fields of a mutable struct

**URL:** <https://discourse.julialang.org/t/how-to-place-an-array-as-one-of-the-fields-of-a-mutable-struct/16939>\
**Category:** New to Julia\
**Created:** [October 29, 2018, 10:27pm UTC](https://discourse.julialang.org/t/how-to-place-an-array-as-one-of-the-fields-of-a-mutable-struct/16939 "2018-10-29T22:27:56Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ralph1](https://avatars.discourse-cdn.com/v4/letter/r/db5fbb/32.png) [@Ralph1](https://discourse.julialang.org/u/Ralph1)\
**Post date:** [October 29, 2018, 10:27pm UTC](https://discourse.julialang.org/t/how-to-place-an-array-as-one-of-the-fields-of-a-mutable-struct/16939/1 "2018-10-29T22:27:56Z")

</div>

I’m a beginner. I’m trying to create a mutable struct with three Float64 fields and one Array of three Float64s. Is there a way to do this, and what is the format.

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [October 29, 2018, 10:33pm UTC](https://discourse.julialang.org/t/how-to-place-an-array-as-one-of-the-fields-of-a-mutable-struct/16939/2 "2018-10-29T22:33:36Z")

</div>

Have you read through [Types · The Julia Language](https://docs.julialang.org/en/v1/manual/types/#Composite-Types-1)? Any specific questions you have after reading it?

---

<div class="post-metadata">

**Author:** ![Sean\_McBane](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sean_mcbane/32/4580_2.png) [@Sean\_McBane](https://discourse.julialang.org/u/Sean_McBane)\
**Post date:** [October 29, 2018, 10:50pm UTC](https://discourse.julialang.org/t/how-to-place-an-array-as-one-of-the-fields-of-a-mutable-struct/16939/3 "2018-10-29T22:50:12Z")

</div>

Are you just confused about how to specify the type of the Array? The `Array` type included in `Base` does not encode information about how many elements it has in the type, so you would declare your struct like

```julia
mutable struct S
    x::Float64
    y::Float64
    z::Float64
    arr::Vector{Float64} # alias for Array{Float64,1}
end

```

and in the constructor `S(...)` you would size `arr` to hold three elements.

If you want a statically sized array that _always_ has three elements, you want to use the package `StaticArrays` (which should really be a stdlib imo, but I digress…). A mutable vector with a fixed size can be specified using an `MVector{N, T}`. Using `StaticArrays` your struct is:

```julia
using StaticArrays

mutable struct S
    x::Float64
    y::Float64
    z::Float64
    arr::MVector{3, Float64}
end

```

`SVector{N, T}` has the same interface, but is not mutable. It is faster if it fits your use case because it does not have to be stored on the heap. Read the `StaticArrays` docs for other details.

---

<div class="post-metadata">

**Author:** ![Ralph1](https://avatars.discourse-cdn.com/v4/letter/r/db5fbb/32.png) [@Ralph1](https://discourse.julialang.org/u/Ralph1)\
**Post date:** [October 29, 2018, 11:07pm UTC](https://discourse.julialang.org/t/how-to-place-an-array-as-one-of-the-fields-of-a-mutable-struct/16939/4 "2018-10-29T23:07:22Z")

</div>

Thank you for your answer. That is exactly what I was looking for. Thank you Again!

---

<div class="post-metadata">

**Author:** ![Sean\_McBane](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sean_mcbane/32/4580_2.png) [@Sean\_McBane](https://discourse.julialang.org/u/Sean_McBane)\
**Post date:** [October 29, 2018, 11:10pm UTC](https://discourse.julialang.org/t/how-to-place-an-array-as-one-of-the-fields-of-a-mutable-struct/16939/5 "2018-10-29T23:10:03Z")

</div>

Glad it helped. I remember coming to Julia a few years ago (StaticArrays was FixedSizeArrays back then…) with mainly C programming experience, and being confused as to how one was supposed to create useful data structures without the equivalent of `double x[3]`, etc.

---

<div class="post-metadata">

**Author:** ![Ralph1](https://avatars.discourse-cdn.com/v4/letter/r/db5fbb/32.png) [@Ralph1](https://discourse.julialang.org/u/Ralph1)\
**Post date:** [October 29, 2018, 11:11pm UTC](https://discourse.julialang.org/t/how-to-place-an-array-as-one-of-the-fields-of-a-mutable-struct/16939/6 "2018-10-29T23:11:21Z")

</div>

Thanks, you saved me a lot of time! Have a great day.
