String array in solidity

Fabio picture Fabio · Mar 10, 2017 · Viewed 14.6k times · Source

I came across quite a common problem that it seems I can't solve elegantly and efficiently in solidity.

I've to pass an arbitrary long array of arbitrary long strings to a solidity contract.

In my mind it should be something like

function setStrings(string [] row)

but it seems it can't be done.

How can I solve this problem?

Answer

Dat Nguyen picture Dat Nguyen · Mar 21, 2017

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) {...}