String array in solidity
This is a limitation of Solidity, and the reason is that string
is basically an arbitrary-length byte array (i.e. byte[]
), and so string[]
is a two-dimensional byte array (i.e. byte[][]
). According to Solidity references, two-dimensional arrays as parameters are not yet supported.
Can a contract function accept a two-dimensional array?
This is not yet implemented for external calls and dynamic arrays - you can only use one level of dynamic arrays.
One way you can solve this problem is if you know in advanced the max length of all of your strings (which in most cases are possible), then you can do this:
function setStrings(byte[MAX_LENGTH][] row) {...}
December 2021 Update
As of Solidity 0.8.0, ABIEncoderV2
, which provides native support for dynamic string arrays, is used by default.
pragma solidity ^0.8.0;
contract Test {
string[] public row;
function getRow() public view returns (string[] memory) {
return row;
}
function pushToRow(string memory newValue) public {
row.push(newValue);
}
}