15

If I try to define a function using an array of strings as an argument, solc gives me the following error:

Error: Internal type is not allowed for public or external functions.
    function test(string[] x){}
                  ^--------^

This works fine:

function test(int[] x){}

And so does this:

function  test(string[] x) private {}

Am I missing something obvious?

2 Answers 2

13

No, I don't believe so. As I understand it, Solidity doesn't have a built-in way yet to deserialize string arrays. It can cheat when the contract is passing an array it created itself to itself, which is why it works with private functions. But if you want to take string arrays from the outside, you're going to have to handle the serialization and deserialization from a string (or bytes) yourself. (E.g., concatenating fixed-length items, or using null characters as separators, or using commas, etc.)

1
  • 1
    Perhaps this could be added to ethereum.stackexchange.com/q/305/42 since taking an array of strings is easily and commonly done in most other languages.
    – eth
    Commented Feb 26, 2016 at 23:05
4

You can convert the array elements to a byte string and then deserialize that byte string back to the array inside the function. Although this can prove to be quite expensive you can try it if you don't have a choice. You can follow this short article to serialize/deserialize any datatype in solidity.

2
  • why was this answer downvoted? It does describe a workaround that should help, even if gas cost could be more. Commented Jul 6, 2018 at 7:40
  • Just because I added the link to the answer rather than writing the whole article here. Commented Aug 4, 2018 at 10:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.