From what I have read, an array is made by using square brackets[],(The type of an array of fixed size k and element type T is written as T[k], and an array of dynamic size as T[].)*. When looking at this basic cryptocurrency example from solidity.readthedocs **, I cannot see any array being made, yet Function mint() seems to refer to an array (balances[receiver] += amount;)

can anyone explain what is going on? what am I missing?

contract Coin {

    address public minter;
    mapping (address => uint) public balances;

    event Sent(address from, address to, uint amount);

    function Coin() {
        minter = msg.sender;
    function mint(address receiver, uint amount) returns  {
        if (msg.sender != minter) return;
        balances[receiver] += amount;
    function send(address receiver, uint amount) {
        if (balances[msg.sender] < amount) return;
        balances[msg.sender] -= amount;
        balances[receiver] += amount;
        Sent(msg.sender, receiver, amount);

*https://solidity.readthedocs.io/en/develop/types.html?highlight=address#arrays **https://solidity.readthedocs.io/en/v0.3.2/introduction-to-smart-contracts.html


The square brackets are not only used for arrays. They are also used for the type mapping.

You refer to items in a mapping the same way you'd refer to items in an array, with myMapping[element] but the data structures are quite different. Mappings are basically hash tables and they can't be iterated so you have to know the right key to get the data out of the mapping.

You can read more about mappings here: https://solidity.readthedocs.io/en/v0.5.3/types.html#mapping-types

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.