I want to define an in-memory 2-dimensional array without fully hard-coding its shape. Let's say I want a 3xN matrix (and to explain ill also set its 0th element to [1, 2, 3]
):
function makeArr (int n){
int[3][n] memory arr;
arr[0] = [1, 2, 3]
}
makeArr(1)
will fail with the message Invalid array length, expected integer literal or constant expression.
I tried defining the array like this:
int[3][] memory arr;
But that also doesn't work, because the array isn't long enough: reverted with panic code 0x32 (Array accessed at an out-of-bounds or negative index)
This question has been asked before, however the answer doesn't seem to have been accepted: Creating a 2D in-memory array in Solidity.
In another similar question, the answers at the time said this simply isn't possible (3yrs old). Returning 2D array with dynamic sizes from a function
Is there an answer yet or a best practice work-around?