When I use the below contract in solidity.
struct Funder {
address addr;
uint amount;
}
contract FundContract
{
struct Campaign {
uint numFunders;
mapping (uint => Funder) funders;
}
mapping (uint => Campaign) campaigns;
function updatestruct(uint campaignID) public
{
Campaign storage c = campaigns[campaignID];
c.funders[c.numFunders++] = Funder(msg.sender, 100); // How this is fine ?
Funder storage f = Funder(msg.sender, 100); // Why this is a compilation error
}
}
As per the solidity documentation, the line
c.funders[c.numFunders++] = Funder(msg.sender, 100);
creates a new temporary memory struct, initialized with the given values, and copies it over to local storage c.
Similarly, I tried the below line
Funder storage f = Funder(msg.sender, 100);
I was expecting the same, however, I get the compilation error instead " Type struct Funder memory is not implicitly convertible to expected type struct Funder storage pointer."
Why is there a compilation error here, as I expected it to create a temp memory and copy it over to the local storage f like the previous one with local storage c?
Are some details available as to how solidity struct memory works in both cases?