# C-like array inside a struct

**URL:** <https://forums.swift.org/t/c-like-array-inside-a-struct/42249>\
**Category:** Using Swift\
**Created:** [November 24, 2020, 11:29am UTC](https://forums.swift.org/t/c-like-array-inside-a-struct/42249 "2020-11-24T11:29:38Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jeyries](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jeyries/32/9113_2.png) [@jeyries](https://forums.swift.org/u/jeyries)\
**Post date:** [November 24, 2020, 11:29am UTC](https://forums.swift.org/t/c-like-array-inside-a-struct/42249/1 "2020-11-24T11:29:38Z")

</div>

Hello all,

Does anyone know if there is a way to have a C-like array inside a struct ?

I would like to have the memory for the array allocated alongside the struct, not somewhere else on the heap. The goal is to avoid the extra memory allocation because of the array.

It is fine if the array can not grow like Swift array, of course.

```swift
struct MyStruct {
    var myValue: Int
    var myArray: [Int8] = .init(repeating: 0, count: 100)
    // ^ here, I would like the items of myArray allocated inside MyStruct
}

```

---

<div class="post-metadata">

**Author:** ![Morten\_Bek\_Ditlevsen](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/morten_bek_ditlevsen/32/28133_2.png) [@Morten\_Bek\_Ditlevsen](https://forums.swift.org/u/Morten_Bek_Ditlevsen)\
**Post date:** [November 24, 2020, 12:09pm UTC](https://forums.swift.org/t/c-like-array-inside-a-struct/42249/2 "2020-11-24T12:09:11Z")

</div>

I think that people generally use (big) tuples for that purpose.

---

<div class="post-metadata">

**Author:** ![jeyries](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jeyries/32/9113_2.png) [@jeyries](https://forums.swift.org/u/jeyries)\
**Post date:** [November 24, 2020, 1:02pm UTC](https://forums.swift.org/t/c-like-array-inside-a-struct/42249/3 "2020-11-24T13:02:25Z")

</div>

Thanks Morten !

Any other ways than tuples ? I cannot imagine to have a tuple containing 100 items for example.

Furthermore there is no subscript available on tuples .

I wonder why something so simple like that is not in the Swift language. Maybe some Swift guru could explain the reason why ??

---

<div class="post-metadata">

**Author:** ![lukasa](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/lukasa/32/1232_2.png) [@lukasa](https://forums.swift.org/u/lukasa)\
**Post date:** [November 24, 2020, 1:07pm UTC](https://forums.swift.org/t/c-like-array-inside-a-struct/42249/4 "2020-11-24T13:07:42Z")

</div>

Tuples are the way to do it. There have been a number of proposals for alternatives, such as [Proposal: Contiguous Variables (A.K.A. Fixed Sized Array Type)](https://forums.swift.org/t/proposal-contiguous-variables-a-k-a-fixed-sized-array-type/1214). I don't believe there is a profound reason that it isn't present, but it causes trickiness in the type system, and just generally has not been worked through at this time.

---

<div class="post-metadata">

**Author:** ![jeyries](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jeyries/32/9113_2.png) [@jeyries](https://forums.swift.org/u/jeyries)\
**Post date:** [November 24, 2020, 1:28pm UTC](https://forums.swift.org/t/c-like-array-inside-a-struct/42249/5 "2020-11-24T13:28:09Z")

</div>

Thanks Cory for the link to this proposal, I hope this will be adopted one day ...

Following the discussions in the forum, I have gathered some interesting ideas about fixed size arrays:

- [A hack for fixed-size arrays in Swift](https://oleb.net/blog/2017/12/fixed-size-arrays/)
- [Fixed size array hacks](https://forums.swift.org/t/fixed-size-array-hacks/32962)
- [Static Arrays](https://forums.swift.org/t/static-arrays/35632)
- [Fixed-size, scoped-storage array manifesto, Version 3](https://forums.swift.org/t/fixed-size-scoped-storage-array-manifesto-version-3/34427)
