# Understaing vector (or array?) fields in julia mutable struct

**URL:** <https://discourse.julialang.org/t/understaing-vector-or-array-fields-in-julia-mutable-struct/79479>\
**Category:** New to Julia\
**Tags:** question, vector, mutable-structure\
**Created:** [April 14, 2022, 9:32am UTC](https://discourse.julialang.org/t/understaing-vector-or-array-fields-in-julia-mutable-struct/79479 "2022-04-14T09:32:13Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![francesco123212](https://avatars.discourse-cdn.com/v4/letter/f/bbe5ce/32.png) [@francesco123212](https://discourse.julialang.org/u/francesco123212)\
**Post date:** [April 14, 2022, 9:32am UTC](https://discourse.julialang.org/t/understaing-vector-or-array-fields-in-julia-mutable-struct/79479/1 "2022-04-14T09:32:13Z")

</div>

Hello everyone! I’m a mechanical engineer coming from MATLAB and relatively new to Julia, so I’m really sorry for my heresies!

I’m currently working on mutable structures, and I’m interested in creating an array (or vector?) of structures like the following:

```julia
    mutable struct s3_str
        c::Int64
        d::Float64
        s3() = new(0,0.)
    end
  mutable struct s2_str
      a::Int64
      b::Array{Int64,1}
      s3 #I would like to have a vector field of s3 structures here
      s2() = new(0, [0], s3()) # is it right?
  end
mutable struct s1
    s2 #I would like to have a vector field of s2 structures here
    s1() = new(s2()) # is it right?
end

```

that I would like to call like:

```julia
s1[i].b = some array of integers
s1[i].s2[j].c = some integer

```

Could you please tell me:

1. how to define the fields of the structs
2. how to preallocate the array of structures s1[-] of length n, containing the array of structures s2[-] of length m with zero-ed (or undefined) values.

Thank you all in advance, and I hope you are all healthy!

---

<div class="post-metadata">

**Author:** ![goerch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerch/32/29122_2.png) [@goerch](https://discourse.julialang.org/u/goerch)\
**Post date:** [April 14, 2022, 9:44am UTC](https://discourse.julialang.org/t/understaing-vector-or-array-fields-in-julia-mutable-struct/79479/2 "2022-04-14T09:44:19Z")

</div>

I tried to make minimal changes to get the script working:

```julia
mutable struct S3_str
    c::Int64
    d::Float64
    S3_str() = new(0,0.)
end
mutable struct S2_str
  a::Int64
  b::Array{Int64,1}
  s3::Vector{S3_str} #I would like to have a vector field of s3 structures here
  S2_str() = new(0, [0], S3_str[]) 
end
mutable struct S1
s2::Vector{S2_str} #I would like to have a vector field of s2 structures here
S1() = new(S2_str[]) 
end

s1 = S1()
push!(s1.s2, S2_str())
s1.s2[1].b = Int[1, 2, 3]
push!(s1.s2[1].s3, S3_str())
s1.s2[1].s3[1].c = 4
@show s1

```

yielding

```julia
s1 = S1(S2_str[S2_str(0, [1, 2, 3], S3_str[S3_str(4, 0.0)])])

```

---

<div class="post-metadata">

**Author:** ![francesco123212](https://avatars.discourse-cdn.com/v4/letter/f/bbe5ce/32.png) [@francesco123212](https://discourse.julialang.org/u/francesco123212)\
**Post date:** [April 14, 2022, 12:20pm UTC](https://discourse.julialang.org/t/understaing-vector-or-array-fields-in-julia-mutable-struct/79479/3 "2022-04-14T12:20:56Z")

</div>

Thanks a lot!
