4

I've seen no documentation on whether this is possible or not, and when I tried the following code, I get a revert error:

pragma solidity ^0.4.0;

contract Hello {

    event TestEvent(uint256[] array);

    function hello() public {
        uint256[] memory output;
        for (uint i = 0; i < 5; i++) {
            output[i] = i;
        }
        TestEvent(output);
    }

}

The compiler raises no warnings about this code, so I'm unsure if it's some other issue or because I'm trying to pass an array to an event.

1 Answer 1

11

Yes, you can pass an array as an event parameter. Your current code is broken because you're accessing output with an index that's out of bounds.

You might be tempted to fix this with output.push(i), but per https://solidity.readthedocs.io/en/develop/types.html#allocating-memory-arrays:

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.

You instead need to allocate the array with the right size to start with. This code works:

pragma solidity ^0.4.19;

contract Hello {

    event TestEvent(uint256[] array);

    function hello() public {
        uint256[] memory output = new uint256[](5);
        for (uint256 i = 0; i < 5; i++) {
            output[i] = i;
        }
        TestEvent(output);
    }
}

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.