# Writing array of structs into storage

**URL:** <https://forum.soliditylang.org/t/writing-array-of-structs-into-storage/1962>\
**Category:** Uncategorized\
**Created:** [November 4, 2023, 6:19pm UTC](https://forum.soliditylang.org/t/writing-array-of-structs-into-storage/1962 "2023-11-04T18:19:42Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![todesstille](https://avatars.discourse-cdn.com/v4/letter/t/ac91a4/32.png) [@todesstille](https://forum.soliditylang.org/u/todesstille)\
**Post date:** [November 4, 2023, 6:19pm UTC](https://forum.soliditylang.org/t/writing-array-of-structs-into-storage/1962/1 "2023-11-04T18:19:42Z")

</div>

I’m trying to write an array of structs (got from calldata) into the storage, but getting:

```auto
Copying of type struct calldata[] calldata to storage not yet supported.

```

Struct itself contains only static elements (enum, address and uint256).

Is there any simple way to do it, without pushing one by one in a loop?

If no, i could push it one by one, but what to do, if i want to rewrite it afterwards, without deleting all the elements? I could set its length to 0 via assembly, is there more natural ways?

---

<div class="post-metadata">

**Author:** ![barakman](https://sea1.discourse-cdn.com/flex001/user_avatar/forum.soliditylang.org/barakman/32/830_2.png) [@barakman](https://forum.soliditylang.org/u/barakman)\
**Post date:** [November 4, 2023, 8:36pm UTC](https://forum.soliditylang.org/t/writing-array-of-structs-into-storage/1962/2 "2023-11-04T20:36:21Z")

</div>

> [@todesstille](#):
>
> but what to do, if i want to rewrite it afterwards, without deleting all the elements?

You can just do something like:

```auto
function func(Item[] calldata src) {
    uint256 length = src.length < dst.length ? src.length : dst.length;

    uint256 i = 0;

    for (; i < length; i++) {
        dst[i] = src[i];
    }

    for (; i < src.length; i++) {
        dst.push(src[i]);
    }

    for (; i < dst.length; ) {
        dst.pop();
    }
}

```

You might be able to take it a bit further and replace that last `for` loop with:

```auto
assembly { sstore(dst, i) }

```

I know that it works for dynamic arrays in memory (using `mstore`).  
But I haven’t verified it for dynamic arrays in storage (using `sstore`).  
So I’m leaving it for you to try it out…

But TBH, calling `pop` might actually give the caller some gas back, since it zerofies storage.  
So it’s probably best to measure the gas cost of each option before deciding which one is better.

---

<div class="post-metadata">

**Author:** ![Charels](https://sea1.discourse-cdn.com/flex001/user_avatar/forum.soliditylang.org/charels/32/1080_2.png) [@Charels](https://forum.soliditylang.org/u/Charels)\
**Post date:** [November 7, 2023, 6:17am UTC](https://forum.soliditylang.org/t/writing-array-of-structs-into-storage/1962/3 "2023-11-07T06:17:04Z")

</div>

Always seen you enthusiastically helping others and answering questions on the forum. Your experience and insights are truly admirable!
