14

I simply want to create an array with stuff in it. Everything I try is an error message in mist. Even this code snippet I got straight from the docs wont compile:

contract C {
    uint someVariable;
    uint[] data;
    function f() {
        uint[] x = data;
        x.push(2);
    }
}

I simply want to do something that would be analogous to this ruby:

arr = ['0x36eaf79c12e96a3dc6f53426c', '0xf235aa56dd96bda02acfb361e']

I dont need the size to be dynamic, and the type is address.

3 Answers 3

14

You're doing something wrong. This works perfectly fine. Try it in the browser compiler.

contract Test {
    address[] public arr =  [0x36eaf79c12e96a3dc6f53426c, 0xf235aa56dd96bda02acfb361e];
}
4
  • Oh, I see the issue. If you quote the addresses in the array literal it will assume it's a string array. That's probably what was causing problems. Gonna make an issue. Commented Mar 21, 2016 at 10:29
  • 1
    Actually, no. That's probably the intended behavior. Addresses can't be assigned quoted hex-strings either. Commented Mar 21, 2016 at 10:40
  • I get an error - "Type uint104[2] memory is not implicitly convertible to expected type address[] storage ref"
    – Dev Oskii
    Commented Feb 1, 2021 at 8:13
  • Either you declare a fixed array address[2] arr=[0x.., 0x..] or declare a dynamic array address[] arr then assign arr[0]=0x...;
    – daniel p
    Commented Feb 10, 2022 at 8:54
3

Here's an example I created using testrpc for this question:

After setting up testrpc and attaching geth:

testrpc -a 10
geth attach rpc:http://localhost:8545

Deploy the following contract on the geth command line:

pragma solidity ^0.4.6;
contract AddressList {  
    event addressregistered(address addy);

    uint public NoMembers; 
    address[] public MemberAddresses;

    function registerAddress() { 
        MemberAddresses.push(msg.sender);    
        NoMembers = MemberAddresses.length;    
        addressregistered(msg.sender);
        }  
}

var addresslistContract = web3.eth.contract([{"constant":false,"inputs":[],"name":"registerAddress","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"NoMembers","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"","type":"uint256"}],"name":"MemberAddresses","outputs":[{"name":"","type":"address"}],"payable":false,"type":"function"},{"anonymous":false,"inputs":[{"indexed":false,"name":"addy","type":"address"}],"name":"addressregistered","type":"event"}]);
var addresslist = addresslistContract.new(
   {
     from: web3.eth.accounts[0], 
     data: '0x606060405234610000575b610260806100196000396000f30060606040526000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff1680631832425f14610054578063525af8601461006357806392fecc4714610086575b610000565b34610000576100616100e3565b005b34610000576100706101f1565b6040518082815260200191505060405180910390f35b34610000576100a160048080359060200190919050506101f7565b604051808273ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200191505060405180910390f35b6001805480600101828181548183558181151161012c5781836000526020600020918201910161012b91905b8082111561012757600081600090555060010161010f565b5090565b5b505050916000526020600020900160005b33909190916101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff160217905550506001805490506000819055507feb8ae6c1136c030439e9ea9ffc5cfb3d6daa418c8a5463bc839c4dd4d8382eab33604051808273ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200191505060405180910390a15b565b60005481565b600181815481101561000057906000526020600020900160005b915054906101000a900473ffffffffffffffffffffffffffffffffffffffff16815600a165627a7a723058207e10191d65bf6dfd6a5c15d4fd564ad9b5659a037257ac3d50e20147b3a1fe010029', 
     gas: '4700000'
   }, function (e, contract){
    console.log(e, contract);
    if (typeof contract.address !== 'undefined') {
         console.log('Contract mined! address: ' + contract.address + ' transactionHash: ' + contract.transactionHash);
    }
 })

Then store, for example, 10 addresses in the contract:

for (var x = 0; x < 10; x++){
    addresslist.registerAddress({from:eth.accounts[x],gas:3000000}) 
}
0
address[3] memory users;
users[0] = 0x...;
users[1] = 0x...;
users[2] = 0x...;

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.