6

I am passing a fixed array to a function where a dynamic array is expected.

addType([1,5, 2,5, 3,2, 4,2, 5,1], [6,1], [7,5]);
addType([1,5], [6,1, 2,5, 3,3], [7,5]);

function addType (
    uint8[] memory _build,
    uint8[] memory _input,
    uint8[] memory _output
)
.......

I get an error when compiling

Invalid type for argument in function call. Invalid implicit conversion from uint8[10] memory to uint8[] memory requested

Found a solution in using a similar structure

uint8[] memory _build = new uint8[](10);
    _build[0] = 1;
    _build[1] = 5;
    _build[2] = 2;
    _build[3] = 5;
    _build[4] = 3;
    _build[5] = 2;
    _build[6] = 4;
    _build[7] = 2;
    _build[8] = 5;
    _build[9] = 1;
uint8[] memory _input = new uint8[](2);
    _input[0] = 6;
    _input[1] = 1;
uint8[] memory _output = new uint8[](2);
    _output[0] = 7;
    _output[1] = 5;

addType(_build, _input, _output);

But it is extremely not effective!

Help, please deal with these arrays!

2
  • Please explain your problem.
    – Aniket
    Feb 1, 2019 at 10:28
  • an error occurs when compiling Invalid type for argument in function call. Invalid implicit conversion from uint8[10] memory to uint8[] memory requested
    – I. Simonov
    Feb 1, 2019 at 10:31

1 Answer 1

5

Function addType expects dynamic storage arrays here while you implicitly create fixed-length when passing just [1,2,3].

Try creating explicit dynamic storage array, push there some values and then pass to function.

Good tutorial on topic: https://github.com/willitscale/learning-solidity/blob/master/support/INVALID_IMPLICIT_CONVERSION_OF_ARRAYS.MD

1
  • Began to use just an array[10], and empty elements to fill 0 array[1,1, 2,2, 3,3, 0,0, 0,0]
    – I. Simonov
    Feb 20, 2019 at 14:36

Your Answer

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

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