# Problem with array of structs

**URL:** <https://discourse.julialang.org/t/problem-with-array-of-structs/91614>\
**Category:** General Usage\
**Created:** [December 13, 2022, 6:29pm UTC](https://discourse.julialang.org/t/problem-with-array-of-structs/91614 "2022-12-13T18:29:55Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![teob](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/teob/32/33609_2.png) [@teob](https://discourse.julialang.org/u/teob)\
**Post date:** [December 13, 2022, 6:29pm UTC](https://discourse.julialang.org/t/problem-with-array-of-structs/91614/1 "2022-12-13T18:29:55Z")

</div>

Hello everyone, I have the following problem:

```julia
Base.@kwdef struct s
    a = [0.0,0.0,0.0]
end

vec = fill(s(), 3)

for (indx,v) in enumerate(vec)
    v.a[1] = indx
end

vec

```

retruns:

3-element Vector{s}:  
s([3.0, 0.0, 0.0])  
s([3.0, 0.0, 0.0])  
s([3.0, 0.0, 0.0])

But, I would expect it to return instead:

3-element Vector{s}:  
s([1.0, 0.0, 0.0])  
s([2.0, 0.0, 0.0])  
s([3.0, 0.0, 0.0])

Anyone know how to help me? Thank you all so much in advance!

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [December 13, 2022, 6:34pm UTC](https://discourse.julialang.org/t/problem-with-array-of-structs/91614/2 "2022-12-13T18:34:32Z")

</div>

The problem is `fill()`. See this paragraph in the docs

> Every location of the returned array is set to (and is thus `===` to) the value that was passed; this means that if the value is itself modified, all  
> elements of the filled array will reflect that modification because they’re still that very value. This is of no concern with `fill(1.0, (5,5))` as  
> the value `1.0` is immutable and cannot itself be modified, but can be unexpected with mutable values like — most commonly — arrays. For example,  
> `fill([], 3)` places the very same empty array in all three locations of the returned vector:

---

<div class="post-metadata">

**Author:** ![teob](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/teob/32/33609_2.png) [@teob](https://discourse.julialang.org/u/teob)\
**Post date:** [December 13, 2022, 6:49pm UTC](https://discourse.julialang.org/t/problem-with-array-of-structs/91614/3 "2022-12-13T18:49:34Z")

</div>

Thank you very much, reading the documentation well I solved it using:

```julia
vec = [s() for i in 0:3]

```

My bad, next time I’ll be more careful 🙂 👍
