13

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?

11

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

3

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.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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