TransWikia.com

The below code gets compiled but when foo is called invalid opcode error is thrown

Ethereum Asked by user61878 on October 27, 2020

pragma solidity >=0.5.0 <0.7.0;
contract Mycontract{
    uint256 a;
    struct User
    {  string name; }
    User[] users;
    function foo() public{
      User storage user=users[0];
      user.name="hey";
    }
    function get() public view returns(string memory)
    {   return users[0].name; }
}

One Answer

It is caused by this : User storage user=users[0];

You try to assign to user the first element of the users array but you can't access an empty array.

If you want to push your object into the array you can use array.push().

Correct answer by clement on October 27, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP