1

I have a problem with the below code, specifically samplestruct2

struct samplestruct1 {
    string name;
}
struct samplestruct2{
    string name;
    mapping (int => samplestruct1) s1s;
}

function createSampleStruct2(string name) {
    samplestruct2 s2;
    s2.name = name;
    ...
}

samplestruct2 s2 is giving me a warning : "uninitilized storage pointer"

I also tried samplestruct2 s2 = samplestruct2(name), it gives me also an error

2 Answers 2

4

Because s2 is a struct being instantiated in the function rather than the contract, solidity will make it a storage member of the contract itself. This is unlike other primitive types which default to memory in the local scope of the function. The warning is a prompt to make a proper declaration. Either declare s2 in the contract or use memory to make s2 local to createSampleStruct2(string name)

contract c {
    struct samplestruct1 {
        string name;
    }
    struct samplestruct2{
        string name;
        mapping (int => samplestruct1) s1s;
    }

    samplestruct2 s2; // declared in contract storage scope.

    function setS2Name(string name) {
        s2.name = name;
    }

    function newS1(int key, string name) {
        samplestruct1 memory s1; // using memory for structs before commiting to storage.
        s1.name = name;
        s2.s1s[key] = s1;
    }
}
0

I've Got the solution by creating a mapping to hold samplestruct2. now I can initialize the struct using below:

st3 s3 = s3s[msg.sender];d where s3s is mapping (int => samplestruct2) s3s;

2
  • @BadrBellaj - I'm a new Soldiity developer. Is that "where" statement part of Solidity, or just a comment? I didn't know that Solidity had a SQL style syntax for collection operations. Commented May 4, 2018 at 12:30
  • this is not a sql query and there is no where keyword.
    – Badr Bellaj
    Commented May 4, 2018 at 13:59

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.