0

Why does this not work

struct Structure {
    string _var;
}

Structure[] structure;

function addStructure(string _var) {
    Structure _s = Structure(_var);
    ->> structure.push(_s);
}

while this works

struct Structure {
    string _var;
}

Structure[] structure;

function addStructure(string _var) {
    X
    ->> structure.push(Structure(_var));
}

I saw this in an tutorial but it does not work for me. Thank you for explanation!

1
  • What error are you getting?
    – Andrey
    Commented Feb 28, 2018 at 1:28

1 Answer 1

2

Try using the memory keyword:

struct Structure {
    string _var;
} 

Structure[] structure;

function addStructure(string _var) {
    Structure memory _s = Structure(_var);
    structure.push(_s);
}

The way you wrote it, _s was interpreted as a reference to storage (permanent) memory. The compiler will not allow an implicit conversion of the temporary memory variable into a storage reference. The push copies the memory ref in to the permanent structure array.

1
  • Thank you very much, that solved it and gave me a tip on where to search!
    – dkb
    Commented Feb 28, 2018 at 12:31

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.