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?

Sebi
  • 5,294
  • 6
  • 27
  • 52

4 Answers4

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

0mkar
  • 199
  • 1
  • 5
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

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

niksmac
  • 9,673
  • 2
  • 40
  • 72
  • 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 '16 at 15:08
0

You can simply declare address array this way:

contract Company {
    address[] employees  =  [0x366f53426c1ve52335t, 0xf2acfb61e345e55fe4];
} 
Supta
  • 9
  • 4
  • 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 '17 at 06:53