0
pragma solidity ^0.4.19;

contract students{

    struct students{
        string fname;
        string lname;
        string course;      
    }

    mapping (address => students) studentsMappig;

    address[] public studentsArray;

    function setStudents(address _address, string _fname, string _lname,string _course) public{

        var student = studentsMappig[_address];

        student.fname = _fname;
        student.lname = _lname;
        student.course = _course;
        studentsArray.push(_address) -1;

    }

}

Question: I am pushing _address in studentsArray. This array will store only addresses. where the other data which is mapped will store like fname, lname etc?

1

Yes, studentsArray is and will be solely an array for address type values, whereas your studentsMapping mapping allows you to access a students struct for a specific key (which is an address).

Concerning your code :

  • You might want to change the struct name from students to Student as a matter of readability.
  • var isn't a valid type in Solidity. In this case, it has to be the type of the struct you are using.
  • Student student = studentsMappig[_address]; won't modify the corresponding struct because, by default, everything is done in memory, it's a bit like RAM. If you desire persistent changes, you need to precise you want to write to storage by adding the keyword : Student storage student = studentsMappig[_address];
  • I think there is a misunderstanding, OP wants to save the struct into the mapping, not as an independent variable as your answer does, I added an answer to save the struct in the mapping. – Jaime Apr 4 '18 at 10:07
  • wow, why are you upset? Instead of taking time to write this angry response, you could just point out why I am wrong. – Jaime Apr 4 '18 at 10:14
  • Not upset. Shared a link under your answer. – Eli Drion Apr 4 '18 at 10:14
0

You are saving the _address in your studentArray. You haven't saved the student data yet. Based on @Eli's Answer you just need to modify one line:

Use Students storage student = studentsMappig[_address]; instead of var student = studentsMappig[_address];

contract students{

struct Students{
    string fname;
    string lname;
    string course;      
}

mapping (address => Students) studentsMappig;

address[] public studentsArray;

function setStudents(address _address, string _fname, string _lname,string _course) public{

    Students storage student = studentsMappig[_address];

    student.fname = _fname;
    student.lname = _lname;
    student.course = _course;
    studentsArray.push(_address);

}

}

I hope this helps you.

  • Less lines != less gas. By doing what you do, it ends up more costly. – Eli Drion Apr 4 '18 at 10:04
  • what? can you elaborate this? Your answer does not solve the problem OP want to save the structure in the mapping but you are creating a struct in the storage independently. Please let me know if I am wrong. – Jaime Apr 4 '18 at 10:10
  • "You haven't saved the student data yet. " how can I store this in array in my case. i am very new in solidity and reading all answers with keen interest. Thanks – Shahzad Malik Apr 4 '18 at 10:17
  • @ Malik, Eli Dirion answer is correct and mine is correct as well, however Eli's answer is cheaper in terms of gas. I modified my answer usind ELi's answer to give you the full code. This is the reason I am in this forum, to learn. I hope this will help you. – Jaime Apr 4 '18 at 10:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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