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

However when the above highlighted statement changed to the below statement will solve the error:

 User memory user=User("hey");  

Can anyone explain the reason behind this changing from memory to storage?

  • Yeah, storage is for pointing to global (state) variables, and memory is for allocating local variables. In both cases, it applies only to structs and arrays. – goodvibration Jul 29 at 13:49
  • Understood thanks,so if we referring storage in a statement then always it should consist of state variables? – user61878 Jul 29 at 14:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.