Fixed-Size Arrays
Part of the Fundamentals section of Coddy's Solidity journey. Lesson 36 of 53.
An array groups values of the same type. uint256[3] memory declares a temporary array with three elements. Indexes start at zero, so its positions are 0, 1 and 2. Access an element with square brackets.
The statements in this excerpt run inside the provided main function.
uint256[3] memory values = [uint256(5), 8, 12];
console.log(values[1]);Index one selects the second element, eight. The explicit uint256 conversion on the first literal gives the array literal the intended element type. Accessing index three would be out of bounds for this three-element array.
Array indexes start at zero, and the last valid index is length minus one.
Challenge
EasyCreate a uint256[3] memory array from a, b and c in that order. Print the third element, then the first.
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 view {
// Write your solution here.
}
}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