# How to initialize an array of structs

**URL:** <https://discourse.julialang.org/t/how-to-initialize-an-array-of-structs/83674>\
**Category:** Performance\
**Tags:** arrays, struct\
**Created:** [July 2, 2022, 10:34pm UTC](https://discourse.julialang.org/t/how-to-initialize-an-array-of-structs/83674 "2022-07-02T22:34:44Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![fft](https://avatars.discourse-cdn.com/v4/letter/f/e8c25b/32.png) [@fft](https://discourse.julialang.org/u/fft)\
**Post date:** [July 2, 2022, 10:34pm UTC](https://discourse.julialang.org/t/how-to-initialize-an-array-of-structs/83674/1 "2022-07-02T22:34:45Z")

</div>

I want to create an array of structs like shown below

```julia
mutable struct mystruct
    a::Float32
    b::Float32
    c::UInt32
end

mystruct() = mystruct(0,0,0)

size = 3

a = Array{mystruct, 3}(undef, size, size, size)

for i in eachindex(a)
    a[i] = mystruct()
end

```

Is there anyway to initialize the structs in the array without looping over the array? For instance something like:

```julia
a = Array{mystruct, 3}({0,0,0}, size, size, size)
# or
a = Array{mystruct, 3}(mystruct(), size, size, size)

```

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [July 2, 2022, 10:50pm UTC](https://discourse.julialang.org/t/how-to-initialize-an-array-of-structs/83674/2 "2022-07-02T22:50:31Z")

</div>

Technically there’s going to be an underlying loop somewhere, but the `fill(x, dims...)` and `fill!(A, x)` methods do this. Just bear in mind that they assign the same single instance `x` to all the elements, so if the instance is mutable and you mutate it, all the elements see the change.

If you want each element to have different instances, an explicit loop is the general way, but judging from your example, you could reasonably define a `Base.zero(::Type{mystruct}) = mystruct(0,0,0)` method, then call `zeros(mystruct, dims...)`. Note that `zeros` takes the type instead of an instance as an argument, so it can do `zero(mystruct)` in the underlying loop to make a new instance per element.

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [July 2, 2022, 10:54pm UTC](https://discourse.julialang.org/t/how-to-initialize-an-array-of-structs/83674/3 "2022-07-02T22:54:56Z")

</div>

Or a comprehension:

```julia
[mystruct() for _ in 1:n1, j in _ 1:n2, k in _ 1:n3]

```

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [July 2, 2022, 11:25pm UTC](https://discourse.julialang.org/t/how-to-initialize-an-array-of-structs/83674/4 "2022-07-02T23:25:35Z")

</div>

Note that a comprehension evaluates `mystruct()`, or whatever code you write there, per iteration just like a for-loop body, so each element gets a different instance. Although it may look more similar to the `fill(mystruct(), dims...)` call at a glance, the difference there is `mystruct()` get evaluated only once, _then_ the returned instance is passed as an argument into a `fill` call.

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [July 2, 2022, 11:32pm UTC](https://discourse.julialang.org/t/how-to-initialize-an-array-of-structs/83674/5 "2022-07-02T23:32:35Z")

</div>

Sure, if the structure is mutable most likely different instances is what the OP wants.

---

<div class="post-metadata">

**Author:** ![fft](https://avatars.discourse-cdn.com/v4/letter/f/e8c25b/32.png) [@fft](https://discourse.julialang.org/u/fft)\
**Post date:** [July 3, 2022, 12:24am UTC](https://discourse.julialang.org/t/how-to-initialize-an-array-of-structs/83674/6 "2022-07-03T00:24:03Z")

</div>

Thanks for your input. I understand there is going to be a loop somewhere I just wanted a convenient way to make initialize it without having to explicitly write the loop.

Different instances for each element is what I want so either defining a new zero function or the comprehension is the answer.

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [July 3, 2022, 9:08am UTC](https://discourse.julialang.org/t/how-to-initialize-an-array-of-structs/83674/7 "2022-07-03T09:08:52Z")

</div>

You can simply use dot assigment with a scalar on the right side:

```julia
a .= Ref(mystruct(0,0,0))

```

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [July 3, 2022, 6:58pm UTC](https://discourse.julialang.org/t/how-to-initialize-an-array-of-structs/83674/8 "2022-07-03T18:58:25Z")

</div>

That will still assign the same instance to every index.

---

<div class="post-metadata">

**Author:** ![leoflotor](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leoflotor/32/35345_2.png) [@leoflotor](https://discourse.julialang.org/u/leoflotor)\
**Post date:** [September 8, 2023, 3:15am UTC](https://discourse.julialang.org/t/how-to-initialize-an-array-of-structs/83674/9 "2023-09-08T03:15:59Z")

</div>

I had a similar problem but my struct starts with its fields **already initialized** due to my use case. So I created an array `a` that accepts only instances of `mystruct`, and I know beforehand the length `a`. Instead of iterating over each element you can use `map` to each index of `a` as:

```julia
a = Vector{mystruct}(under, 5)
map(x -> a[x] = mystruct(), eachindex(a))

```

The result is the same as what you initially proposed but I found that `fill` and `fill!` populate the array with the same instance of `mystruct`. And I could not use `zeros` because the fields are initialized beforehand.

Using dot assignment as mentioned by @rafael.guerra fills `a` with the same instance of `mystruct`. If you modify a field, for example, from `a[1]`, then the rest of the entries in `a` for that field will have the same value.

Also, as pointed out by @lmiq, the list comprehension also gets the job done as you want, but I like using `map` better.

---

<div class="post-metadata">

**Author:** ![yolhan\_mannes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yolhan_mannes/32/220485_2.png) [@yolhan\_mannes](https://discourse.julialang.org/u/yolhan_mannes)\
**Post date:** [September 9, 2023, 12:47pm UTC](https://discourse.julialang.org/t/how-to-initialize-an-array-of-structs/83674/10 "2023-09-09T12:47:01Z")

</div>

Maybe something like this :

```julia
mutable struct mystruct
    a::Float32
    b::Float32
    c::UInt32
end

mystruct() = mystruct(0,0,0)

Base.:zero(T ::Type{mystruct}) = mystruct()
size = 3
a = zeros(mystruct, (size,size,size))
println(a)
println(a[1,1,1])

```
