[Ethereum] Solidity: Initialize struct containing string array

arrayssoliditystringstruct

I do not seem to be able to initialize a struct containing a string array. This is what I tried:

contract testStruct {
    struct stru{
        string[] s;
    }

    stru myStru;

    /*
    function add(string s) {
        string[] strAr; // Uninitialized storage pointer. Did you mean ' memory strAr'?
        strAr.push(s);
        myStru = stru(strAr);
    }
    */

    function add(string s) {
        string[] memory strAr; // just doesnt do anything, exception?
        strAr[0] = s;
        myStru = stru(strAr);
    }

    function getFirst(uint i) constant returns (string s) {
        s = myStru.s[0];
    }
}

The first version (commented out) gives me the compiler warning and does not seem to write anything to storage. The second one seems to run into an exception (I assume that looking at the gas consumption). Thus my question is: How do I initialize a structure containing a string array?

Best Answer

The other answer which didnt work brought me on track to find it out myself:

contract testStruct {
    struct stru{
        string[] s;
    }

    stru myStru;

    function add(string s) {
        myStru.s.push(s);
    }

    function getAt(uint256 i) constant returns (string s) {
        s = myStru.s[i];
    }
}
Related Topic