Making array in Solidity

by Ole Kristian   Last Updated May 15, 2019 10:28 AM

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

Tags : solidity


Answers 1


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

Lauri Peltonen
Lauri Peltonen
May 15, 2019 10:06 AM

Related Questions


Updated October 09, 2018 12:28 PM

Updated November 28, 2018 04:28 AM

Updated December 12, 2018 08:28 AM

Updated January 10, 2019 23:28 PM

Updated July 23, 2018 00:28 AM