# Mutable struct with multi dimensional Array as a field

**URL:** https://discourse.julialang.org/t/mutable-struct-with-multi-dimensional-array-as-a-field/6529
**Category:** General Usage
**Created:** [October 18, 2017, 6:40pm UTC](https://discourse.julialang.org/t/mutable-struct-with-multi-dimensional-array-as-a-field/6529 "2017-10-18T18:40:05Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![ArchieCall](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/archiecall/32/206_2.png) [@ArchieCall](https://discourse.julialang.org/u/ArchieCall)
#### Post date: [October 18, 2017, 6:40pm UTC](https://discourse.julialang.org/t/mutable-struct-with-multi-dimensional-array-as-a-field/6529/1 "2017-10-18T18:40:05Z")

</div>

The code below is my attempt at creating a mutable struct that has an Array as a field.

Does this seem to be the correct method of creating the Array?  
It seems a little unnatural of how the length and dims are created after the fact!

```julia
#-- a mutable struct containing two vars and an array
mutable struct MyData
  Hits::Int #-- var1 of type Int
  Skips::Int #-- var2 of type Int
  SomeArray::Array{Int} #-- Array type of Int => Note: no dims or length seem to be allowed
end

#--- intialize VarData in following two lines
VarData = MyData(0, 0, repeat(0:0, outer = 12)) #--- initialize vars to 0, and Array to length 12x1 with val 0
VarData.SomeArray = reshape(VarData.SomeArray, 4,3) #-- reshape Array into desired dims of 4x3
#--- is there a (better) way to combine the preceeding two lines into one line?

```

---

<div class="post-metadata">

### Author: ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)
#### Post date: [October 18, 2017, 6:41pm UTC](https://discourse.julialang.org/t/mutable-struct-with-multi-dimensional-array-as-a-field/6529/2 "2017-10-18T18:41:18Z")

</div>

> [@ArchieCall](#):
>
> SomeArray::Array{Int}

`SomeArray::Array{Int,2}` etc. to be explicit, or `SomeArray::Array{Int,N}` with `mutable struct MyData{N}`

Or are you looking for `VarData = MyData(0, 0, zeros(Int,4,3))`?

---

<div class="post-metadata">

### Author: ![ArchieCall](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/archiecall/32/206_2.png) [@ArchieCall](https://discourse.julialang.org/u/ArchieCall)
#### Post date: [October 18, 2017, 6:58pm UTC](https://discourse.julialang.org/t/mutable-struct-with-multi-dimensional-array-as-a-field/6529/3 "2017-10-18T18:58:47Z")

</div>

I was looking for your zeros alternative. That works!

I thought I had tried it but must have typed it wrong. Thanks again…Arch

---

<div class="post-metadata">

### Author: ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)
#### Post date: [October 18, 2017, 9:48pm UTC](https://discourse.julialang.org/t/mutable-struct-with-multi-dimensional-array-as-a-field/6529/4 "2017-10-18T21:48:13Z")

</div>

This SO question is similar and may also be helpful:

> <https://stackoverflow.com/questions/45069914/defined-type-with-unspecified-dimension-array-in-julia>
