21

This question is related to this one but using address arrays instead of strings. Does solidity support arrays of addresses to be passed as method arguments?

contract C {
    uint256 hexVal = 0xABCD;

    function func(address[] addrs) {
        // Do somethings with addrs
    }
}

If yes, is it passed as a Javascript string array when called from an external app?

1

4 Answers 4

14

Yes solidity support arrays of addresses to be passed as method arguments. Here is an working example

pragma solidity ^0.4.11;

contract AddressStore {
    address[] public bought;

    // set the addresses in store
    function setStore(address[] _addresses) public {
        bought = _addresses;
    }
}

https://ethfiddle.com/gfNfIFcT2C

6

Yes, your code will work, and yes you would call it from Javascript with an array of strings such as [ "0x123...", "0x345..." ].

See it in action here https://github.com/b9lab/array-parameters/blob/master/test/takeArrays.js#L31-L34

2
  • Link only answer is not encouraged and will fall under very low quality answer.
    – niksmac
    Aug 22, 2016 at 12:22
  • 3
    Noted, and updated. Aug 22, 2016 at 14:47
2

As per the documentation

Arrays can have a compile-time fixed size or they can be dynamic. For storage arrays, the element type can be arbitrary (i.e. also other arrays, mappings or structs). For memory arrays, it cannot be a mapping and has to be an ABI type if it is an argument of a publicly-visible function.

Creating arrays with variable length in memory can be done using the new keyword. As opposed to storage arrays, it is not possible to resize memory arrays by assigning to the .length member.

contract C {
    function f(uint len) {
        uint[] memory a = new uint[](7);
        bytes memory b = new bytes(len);
        // Here we have a.length == 7 and b.length == len
        a[6] = 8;
    }
}

Plus read the properties here

1
  • I want to know if an array of addresses can be passed as an argument to a method of the contract like I mentioned in the example in the question. This call is external and not from inside the contract (a method calling another method).
    – Sebi
    Aug 22, 2016 at 15:08
0

You can simply declare address array this way:

contract Company {
    address[] employees  =  [0x366f53426c1ve52335t, 0xf2acfb61e345e55fe4];
} 
1
  • 4
    Welcome to Ethereum Stack Exchange! The attempt to be helpful is appreciated but 2 things to note: 1. doesn't really answer the question 2. does not compile (non-hex characters)
    – eth
    Oct 10, 2017 at 6:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.