As per documentation fixed size memory arrays cannot be assigned to dynamically-sized memory arrays, i.e. the following is not possible:
uint[] memory someDynamicArr = [1,2,3];
This will throw error because the type of [1, 2, 3]
is uint8[3] memory
, because the type of each of these constants is uint8. If you want the result to be a uint[3] memory
type, you need to convert the first element to uint
.
uint[] memory someDynamicArr = [uint(1),2,3];
Following will throw an error too, because fixed size memory arrays cannot be assigned to dynamically-sized memory arrays.
// uint[3] memory cannot be converted to uint[] memory.
uint[] memory x = [uint(1), 3, 4];
If you want to initialize dynamically-sized arrays, you have to assign the individual elements:
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.4.16 <0.9.0;
contract C {
function f() public pure {
uint[] memory x = new uint[](3);
x[0] = 1;
x[1] = 3;
x[2] = 4;
}
}