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?
storage
is for pointing to global (state) variables, andmemory
is for allocating local variables. In both cases, it applies only to structs and arrays.