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");**  
    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. Jul 29 '20 at 13:49
  • Understood thanks,so if we referring storage in a statement then always it should consist of state variables?
    – user61878
    Jul 29 '20 at 14:11

Variable in contract by default are of storage, i.e. they are state variables. And new struct default allocation within function is memory.

Hence the compiler warning.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.