10

How to create arrays addresses using solidity? And we can write two-dimensional array?

bool[2][] m_pairsOfFlags;

Here is an example of the array, but it does not work

16

Yes you can.

This little snippet might help. It's just a little toy to show how the two-dimensional array elements are referenced.

It might be helpful to point out that things may be a little counter-intuitive because the X & Y axis may seem to be reversed.

bool[2][] flags; 

It starts by describing a length 2 array of bools. Then it goes on to say there will be a dynamic list of those called flags.

So, to reference such things, the dynamic dimension is high order.

bool flag = flags[dynamicIndex][lengthTwoIndex];

The above might look backwards, so that can be disorienting.

Some functions I put together to demo this in BrowserSolidity

pragma solidity ^0.4.6;

// set all the inserts to true for simplicity

contract Arrays{

    // dynamic list of length 2 bools
    bool[2][] flags;

    function Arrays() {
        // append a length 2 array to the dynamic list of flags
        flags.push([true,true]);
    }

    function appendFlag() returns(uint length) {
       // append another length 2 array to the dynamic dimension
       return flags.push([true,true]);
    }

    // return a length 2 array stored in the dynamic dimension
    // will throw if index > flags.length-1 (index starts at 0)
    function getFlags(uint index) constant returns(bool[2] flagList) {
        return(flags[index]);
    }

    // return one flag from the array
    function getFlag(uint dynamicIndex, uint lengthTwoIndex) constant returns(bool flag) {
        return flags[dynamicIndex][lengthTwoIndex];
    }

    // return a count of length 2 arrays stored in flags;
    function getFlagsCount() constant returns(uint count) {
        return flags.length;
    }

}

enter image description here

Hope it helps.

  • 1
    Was it a design choice of solidity to reverse the indexes or is it a bug that will be fixed later (or never cuz we're stuck with it)? – Jonah May 6 at 20:54
  • 1
    It's a design choice. – Rob Hitchens - B9lab May 6 at 21:50

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.