13

Using solidity 0.4.15, there is a function that takes a number and returns array of fixed length called traits:

function splitN(uint256 n) constant returns (uint8[12]) {
    uint8[12] memory traits = new uint8[12];
    // alter values in traits ...
    return traits;
}

But this way to initialize traits don't compile, with the errors:

.../Genes.sol:28:39: TypeError: Length has to be placed in parentheses after the array type for new expression.
        uint8[12] memory traits = new uint8[12];
                                      ^-------^
,.../Genes.sol:28:9: TypeError: Type function (uint256) returns (uint8[12] memory) is not implicitly convertible to expected type uint8[12] memory.
        uint8[12] memory traits = new uint8[12];

Then I tried following the message hints, and a bunch of other ways to initialize, but the only way I could make it work was to actually fill an array of 12 zeroes:

    uint8 z = 0;
    uint8[12] memory traits = [z, z, z, z, z, z, z, z, z, z, z, z];

So is there a more elegant way of initializing the array?

2
  • did you try just uint8[12] memory traits; ? Commented Oct 9, 2017 at 2:06
  • 1
    You are right @AchalaDissanayake ! Can you put that as an answer? Commented Oct 11, 2017 at 20:05

4 Answers 4

15

To initialize an empty array instead of uint8[12] memory traits = new uint8[12]; use uint8[12] memory traits;.

Then you can alter the array in the // alter values in traits ... section.

function splitN(uint256 n) constant returns (uint8[12]) {
    uint8[12] memory traits;
    // alter values in traits ...
    return traits;
}
13

In order to initialize an array from memory you have to do it like this:

uint8[] memory theArray = new uint8[](12) 

Where the 12 inside the parenthesis is the array length.

You can also initialize the array as follows:

function getTraits() constant returns (uint8[3]){
      uint8[3] memory traits = [1,2,3];
      return traits;
    }
2
  • 1
    My question is to init uint8[12], this solution errors to return: TypeError: Return argument type uint8[] memory is not implicitly convertible to expected type (type of first return variable) uint8[12] memory. return traits; Commented Oct 8, 2017 at 23:45
  • 1
    You have to change the function's return type to uint8[] to fix that. Alternatively, you could initialize de fixed-size array like I just wrote in my updated answer. Commented Oct 9, 2017 at 0:02
2

Here are two ways to initialize a fixed-size memory array. In solidity docs, it says we need to use "new" operator. But actually, here is how I did without "new" operator (second way). Also another bonus, I added return statements for both ways.

    // Way 1: by using the "new" operator
    function createArr1(uint _n, uint _m, uint _k) external pure returns(uint[] memory) {
        uint[] memory a = new uint[](3);
        a[0] = _n;
        a[1] = _m;
        a[2] = _k;
        return a;
    }
    // Way 2: without "new" operator.
    function createArr2(uint _n, uint _m, uint _k) external pure returns(uint[3] memory) {
        uint[3] memory a = [_n, _m, _k];
        return a;
    }
0

Use the following syntax if you don't know the correct value and initialize using a variable.

function abc(uint[] a) public {
    uint[] memory c = new uint[](a.length);
}

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.