The easy solution was to eliminate a few lines and directly assign the new value. It does say ‘updateBalance’ versus ‘send’ or ‘add’ so I left the assign new balance versus adding it to the old.
The difference between the old/new version:
machine$ diff datalocation.sol datalocation-fix.sol
16,17c16
< User memory user = users[id];
< user.balance = balance;
---
> users[id].balance = balance;
The resulting code…
pragma solidity 0.7.5;
contract MemoryAndStorage {
mapping(uint => User) users;
struct User{
uint id;
uint balance;
}
function addUser(uint id, uint balance) public {
users[id] = User(id, balance);
}
function updateBalance(uint id, uint balance) public {
users[id].balance = balance;
}
function getBalance(uint id) view public returns (uint) {
return users[id].balance;
}
}