Ethereum Stack Exchange is a question and answer site for users of Ethereum, the crypto value and blockchain-based consensus network. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
up vote 4 down vote accepted

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) {
        stru sStruct = myStru;
        sStruct.s.push(s);
    }

    function getAt(uint i) constant returns (string s) {
        s = myStru.s[i];
    }
}
share|improve this answer

Try this approach. Create the struct, then add to the array:

stru sStruct = stru();
sStruct.s.push('hello');
share|improve this answer
1  
Unfortunately that does not compile: Wrong argument count for function call: 0 arguments given but expected 1. stru sStruct = stru(); but you brought me on track, thanks! – Sebastian May 2 at 19:32

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.