0

What is the easiest way to fill a storage array?

uint[4]  temp;
temp = [1,2,3,4];
>> TypeError: Type uint8[4] memory is not implicitly convertible to expected type uint256[4] storage pointer

As far as I understand the first line is read as a storage variable declaration, but inplace array assignment is applicable only to memory variables.

Is there a way to fill temp with specific numbers except for element-by-element assignment?

1 Answer 1

1

Not sure I follow your use case, but I think this has some examples of what you might be trying to do:

pragma solidity ^0.4.19;
contract Test {
   // A an array held in contract storage (permanent)
   uint[3] storage_array;

   function getArray() public view returns(uint[3]){
       return storage_array;
   }

   /*
    * This initializes a temporary array and then copies it into
    * the contract's permanent storage ref (storage_array).
    */
   function setArray() public {
       uint[3] memory tmp = [uint(1),2,3];
       storage_array =tmp;
   }

   /* This initializes a temporary array that only lives
    * its life inside the method
    */
   function fillTemp() public returns (uint[3]) {
       uint[3] memory tmp = [uint(1),2,3];
       return tmp;
   }

}

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.