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?

Achala Dissanayake
  • 5,819
  • 15
  • 28
  • 38
Fabiano Soriani
  • 385
  • 1
  • 4
  • 13

4 Answers4

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;
}
Achala Dissanayake
  • 5,819
  • 15
  • 28
  • 38
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;
    }
pabloruiz55
  • 7,686
  • 2
  • 17
  • 38
  • 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; – Fabiano Soriani Oct 08 '17 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. – pabloruiz55 Oct 09 '17 at 00: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;
    }
Abdulhakim
  • 201
  • 1
  • 6
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);
}
ranvir
  • 51
  • 1
  • 4