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?

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

1 Answer 1


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 and acknowledge you have read our privacy policy.

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