Menu
Coddy logo textTech

Storage Arrays

Part of the Fundamentals section of Coddy's Solidity journey. Lesson 38 of 53.

A dynamic array declared at contract scope lives in storage. Use push(value) to append an element and pop() to remove the last one. Calling pop on an empty array reverts. Its length changes as values are appended or removed.

Declare this at contract scope:

uint256[] values;

Use it inside the provided main function:

values.push(6);
values.push(15);
values.pop();
console.log(values.length);
console.log(values[0]);

The array starts empty in this test. Two pushes create two elements, and pop removes the second one. The first remains six. Updating a storage array changes contract state, so main cannot be view.

push and pop change a dynamic storage array at its end.

challenge icon

Challenge

Easy

Declare a dynamic uint256 storage array named values. Push a, b and c, remove the last element, then print length and the remaining last value.

The tests pass arguments to main in this order: uint256 a, uint256 b, uint256 c. Each test starts with fresh contract state.

Use console.log for the requested output. Print only the requested values, one per line, with no extra labels unless the task specifies them.

Try it yourself

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.26;
import "forge-std/console.sol";
contract Main {
    function main(uint256 a, uint256 b, uint256 c) external  {
        // Write your solution here.
    }
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals

Practice on your own: Solidity playground