Yes
This causes a specific user’s balance to be updated in the mapping. The “pointer” isn’t something that the mapping “has”. It is a reference to a particular place in the mapping, from somewhere else in the program.
users[id]
is the “standard” way to “point to” a specific User instance (a user’s record) in the users
mapping, because the ID is the key to that specific User instance. [id]
references the function’s id
parameter which is input by the function caller, and represents the user whose balance we want to update.
So, probably the most obvious (and easiest to understand) way to update a user’s balance is as follows:
users[id].balance = balance;
balance
to the right of the assignment operator (=
) references the function’s balance
parameter which is input by the function caller, and represents the new balance.
This new balance is assigned to users[id].balance
which is effectively a pointer to where the new balance should be assigned. As explained above users[id]
points to the “record” of a specific user in the users
mapping, and by adding the balance
property, it now points to the specific value held in that property which represents the user’s existing balance. As a result, the user’s balance stored in the mapping is updated.
With User memory user = users[id]
the pointer itself is assigned to a local user
variable, which means that a copy is made, and stored locally in memory, of the specific user’s record which the pointer references in the mapping. After being assigned, the pointer users[id]
is no longer used within the function. So instead of the mapping being updated with the new balance, user.balance = balance
updates the local copy with the new balance. This update is lost when the function is exited, because the change has not been stored permanently in the mapping, but instead only temporarily in memory.
With User storage user = users[id]
the pointer is assigned to the variable user
, and because the data of both user
and the mapping users
is located in storage, this effectively creates a copy of the pointer, and means that within the function both users[id]
and user
will act as identical pointers to the same user’s record in the mapping. This is why, in their own specific context, the following lines of code both update a specific user’s balance in the mapping:
users[id].balance = balance;
user.balance = balance;
Sorry, about the long answer, but it’s very hard to answer your question without explaining the details of what’s actually happening. Anyway, I hope this helps to make it a bit clearer. Understanding these types of concepts is often a gradual process, so don’t be surprised if you still don’t immediately understand this fully. With more study and practice, you’ll then probably find that later on things will suddenly fall into place and click