1

I'm having problem to understand why the method push() does't work on Remix using Solidity 0.6.

I have read the documentation about arrays fixed size and dynamic size doc reference

for example in this code:

pragma solidity ^0.6.0;

contract Prova
{

   function prova() public{

      uint[] memory array;
      array.push(4);

   }
}

I always get the error: :

TypeError: Member "push" is not available in uint256[] memory outside of storage.

Someone can show me sole example about arrays in solidity. I'm learning. Thanks a lot.

1 Answer 1

2

You are trying to use .push with a transient memory instance of an array. It has to be a storage variable.

pragma solidity ^0.6.0;

contract Prova
{
   uint[] public array;

   function prova() public {
      array.push(4);
   }
}

Or, for giggles:

pragma solidity ^0.6.0;

contract Prova
{
   uint[] public array;

   function prova(uint number) public {
      array.push(number);
   }
}

It is declared globally, so storage is implied. I added public so you can inspect it with array(uint row).

Hope it helps.

1
  • ok perfect, my problem was that I declared the array inside the function ... Commented Apr 14, 2020 at 17:15

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.