2

The following code works, that is, I can call add(2,3) and then get(2,0) returns 3:

pragma solidity ^0.4.10;

contract Bar {
    struct Foo{
        uint x;
    }
    mapping(uint => Foo[]) foo;

    function add(uint id, uint _x) public {
        foo[id].push(Foo(_x));
    }

    function get(uint id, uint index) view public returns(uint){
        return foo[id][index].x;
    }
}

This code does not work, that is, I call add(0xB139e6071772FAdC0e1Ad79C63415a2866fD447F,3), and then get(0xB139e6071772FAdC0e1Ad79C63415a2866fD447F,0) returns 0:

pragma solidity ^0.4.10;

contract Bar {
    struct Foo{
        uint x;
    }
    mapping(address => Foo[]) foo;

    function add(address id, uint _x) public {
        foo[id].push(Foo(_x));
    }

    function get(address id, uint index) view public returns(uint){
        return foo[id][index].x;
    }
}

The only difference is that the uint type has been swapped with the address type in a couple of spots.

  • This exact code works fine for me in Remix. If you're still having trouble, please describe how you're deploying the contract and how you're calling the functions. (Share code if you're calling the functions from code.) – smarx Mar 14 '18 at 7:08
  • 3
    If you're using Remix, be sure to enclose the address in double quotes. – smarx Mar 14 '18 at 7:09
1

had to enclose the address in double quotes when using remix as smarx said

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.