Solidity Structs – How to Initialize a Struct with an Empty Array

arrayssoliditystruct

I am trying to initialize this struct and I can't figure out how to pass in a dynamically sized empty array..

      uint8[] id;
  users[msg.sender] = User( true, msg.sender, _username, block.timestamp, block.number, 0, id, 0);

Best Answer

The following will work:

contract Test {
    struct Object {
        uint a;
        string b;
        string[] c;
        mapping(uint => uint) d;
    }

    Object field;

    function Test() {
        field = Object({
            a: 1,
            b: "abc",
            c: new string[](0)
        });
    }
}
Related Topic