# Structure Usage

**URL:** <https://discourse.julialang.org/t/structure-usage/42269>\
**Category:** New to Julia\
**Created:** [June 29, 2020, 9:06pm UTC](https://discourse.julialang.org/t/structure-usage/42269 "2020-06-29T21:06:52Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![smickus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/smickus/32/18002_2.png) [@smickus](https://discourse.julialang.org/u/smickus)\
**Post date:** [June 29, 2020, 9:06pm UTC](https://discourse.julialang.org/t/structure-usage/42269/1 "2020-06-29T21:06:52Z")

</div>

Hi so I am currently trying to create a structure I have included a condensed version of my structure that I am trying to create. The issue is that when I try and set the values of the fields in the structure they are all overwritten with the last value that I have set to the structure and I was wondering how I could stop this.

```julia
MAXBD=5
mutable struct BODYSTRUCTURE
    BODYNAME::String # Units: 'nd', Desc: 'Body Name'
end

mutable struct MULTIBODYSTRUCTURE
    BD::Array{BODYSTRUCTURE,1}
end

bd = BODYSTRUCTURE(
    " ",
    )

M = MULTIBODYSTRUCTURE(
    fill(bd, (MAXBD)),
)

M.BD[1].BODYNAME="first"
M.BD[2].BODYNAME="second"

```

This produces the following:

```julia
julia> M.BD[2].BODYNAME
"second"

julia> M.BD[1].BODYNAME
"second"

```

Instead of the desired:

```julia
julia> M.BD[2].BODYNAME
"first"

julia> M.BD[1].BODYNAME
"second"

```

I would greatly appreciate any assistance with this problem.

---

<div class="post-metadata">

**Author:** ![mcabbott](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcabbott/32/6603_2.png) [@mcabbott](https://discourse.julialang.org/u/mcabbott)\
**Post date:** [June 29, 2020, 9:28pm UTC](https://discourse.julialang.org/t/structure-usage/42269/2 "2020-06-29T21:28:06Z")

</div>

`fill` will re-use the same element multiple times, not make independent copies. Thus:

```julia
julia> tri = fill([1], 3) # the same array, 3 times
3-element Array{Array{Int64,1},1}:
 [1]
 [1]
 [1]

julia> push!(tri[1], 2) # mutate the sole inner array
2-element Array{Int64,1}:
 1
 2

julia> tri
3-element Array{Array{Int64,1},1}:
 [1, 2]
 [1, 2]
 [1, 2]

julia> tri[3] = [3,4,5]; # replace an element of the outer array

julia> tri
3-element Array{Array{Int64,1},1}:
 [1, 2]
 [1, 2]
 [3, 4, 5]

```

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [June 29, 2020, 9:44pm UTC](https://discourse.julialang.org/t/structure-usage/42269/3 "2020-06-29T21:44:02Z")

</div>

What @mcabbott said is right. The way this is often worked around is to use list comprehensions:

```julia
M = MULTIBODYSTRUCTURE(
    collect(deepcopy(bd) for i in 1:MAXBD)
)

```

---

<div class="post-metadata">

**Author:** ![lucas711642](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lucas711642/32/12051_2.png) [@lucas711642](https://discourse.julialang.org/u/lucas711642)\
**Post date:** [June 29, 2020, 9:45pm UTC](https://discourse.julialang.org/t/structure-usage/42269/4 "2020-06-29T21:45:26Z")

</div>

> [@smickus](#):
>
> ```julia
> M.BD[1].BODYNAME="first"
> M.BD[2].BODYNAME="second"
> 
> ```

You should replace these lines by:

```julia
M.BD[1] = BODYSTRUCTURE("first")
M.BD[2] = BODYSTRUCTURE("second")

```

Also, it seems that in your case you want an uninitialized array. Thus you may replace

```julia
M = MULTIBODYSTRUCTURE(
    fill(bd, (MAXBD)),
)

```

by

```julia
M = MULTIBODYSTRUCTURE(
    Array{BODYSTRUCTURE,1}(undef, MAXBD),
)

```
