Exploring Fixed-Size Arrays in Solidity
Arrays are a key part of Solidity programming, allowing developers to store and work with collections of similar elements. Solidity supports two types of arrays: fixed-size arrays and dynamic-size arrays.
In this post, we will focus on fixed-size arrays, including their declaration, initialization, and operations.
What Are Fixed-Size Arrays?
Fixed-size arrays are arrays with a predetermined size, defined at compile time. They can only hold a specific number of elements, making them predictable and efficient for certain use cases.
Characteristics of Fixed-Size Arrays:
Homogeneous elements: All elements in the array must be of the same type (e.g., integers, strings).
Size defined at compile time: The size of the array cannot be changed after it is declared.
Declaration of Fixed-Size Arrays in Solidity
To declare a fixed-size array, specify:
The data type (e.g.,
uint
for unsigned integers).The number of elements in square brackets
[]
.The visibility (e.g.,
public
).The variable name.
uint[number_of_elements] public arr;
Example:
uint[5] public arr; // An array of 5 unsigned integers
In memory, this array will initially have all elements set to their default values (e.g., 0
for integers).
Initialization of Fixed-Size Arrays
You can initialize a fixed-size array with specific values during declaration:
// SPDX-License-Identifier: MIT
pragma solidity >=0.7.0 <0.9.0;
contract demo {
uint[5] public arr = [10, 20, 30, 40, 50];
}
This will store the values in the following order:
Index 0: 10
Index 1: 20
Index 2: 30
Index 3: 40
Index 4: 50
Accessing Elements in Fixed-Size Arrays
You can access elements in an array by their index:
arr[0]; // Returns 10
arr[4]; // Returns 50
Modifying Fixed-Size Arrays Using Functions
To modify elements in a fixed-size array, you can create functions to insert or update values.
Example: Inserting an Element
// SPDX-License-Identifier: MIT
pragma solidity >=0.7.0 <0.9.0;
contract demo {
uint[5] public arr = [10, 20, 30, 40, 50];
function insert(uint index, uint _item) public {
arr[index]=_item;
}
}
This function takes an index
and an item
as arguments and updates the array at the specified index.
Example: Returning an Element
// SPDX-License-Identifier: MIT
pragma solidity >=0.7.0 <0.9.0;
contract demo {
uint[5] public arr = [10, 20, 30, 40, 50];
function insert(uint index, uint _item) public {
arr[index]=_item;
}
function returnArr(uint index) public view returns (uint) {
return arr[index];
}
}
Returning the Entire Array
To return the entire array, you need to specify the memory
keyword, as arrays are reference data types that default to storage.
Example: Returning All Elements
// SPDX-License-Identifier: MIT
pragma solidity >=0.7.0 <0.9.0;
contract demo {
uint[5] public arr = [10, 20, 30, 40, 50];
function insert(uint index, uint _item) public {
arr[index]=_item;
}
function returnArr(uint index) public view returns (uint) {
return arr[index];
}
function returnAllArr() public view returns (uint[5] memory) {
return arr;
}
}
Why Use the Memory
Keyword?
When working with reference data types like arrays in functions, Solidity needs the memory
keyword to show that the function will use a temporary copy in memory. If you don't use it, the compiler will give an error.
Practical Notes
Default Values: Elements in an uninitialized array are automatically set to the default value for their type (e.g.,
0
for integers).Immutability of Size: Once declared, the size of a fixed-size array cannot be changed.
Visibility: Arrays declared as
public
automatically generate a getter function for accessing elements by index.Memory vs. Storage: Arrays at the contract level are stored in
storage
by default, while arrays inside functions can usememory
for temporary operations.
Another Example
// SPDX-License-Identifier: MIT
pragma solidity >=0.7.0 <0.9.0;
contract FixedSizeArray {
uint[5] public arr = [10, 20, 30, 40, 50];
// Insert an element at a specific index
function insert(uint index, uint item) public {
require(index < arr.length, "Index out of bounds");
arr[index] = item;
}
// Get an element by index
function getElement(uint index) public view returns (uint) {
require(index < arr.length, "Index out of bounds");
return arr[index];
}
// Return the entire array
function getAllElements() public view returns (uint[5] memory) {
return arr;
}
}
Summary
Fixed-size arrays are a strong and efficient way to manage groups of similar data in Solidity. Even though their size can't change, their predictability and simplicity make them ideal for some situations.