0

Suppose a struct is made in solidity and pushed the same struct to an array and a mapping (assume a random key), will the change in struct accessed through mapping will affect the change in the same element pushed inside array?

pragma solidity ^0.8.0;

contract StudentVerify {
   struct abc{
    string name;
    uint roll;
    uint8 sem;
   }

    abc[] public Array;
    mapping(uint => abc) public mapper;

    function setter(string memory _name, uint _roll,uint8 _sem) public {
        abc memory f = abc(_name,_roll,_sem);
        mapper[_roll] = f;
        Array.push(f);
    }
    
 function MapSetter( uint _roll,uint8 _sem) public {
        mapper[_roll].sem = _sem;
    }

suppose in the example I changed the semester of a student accessed through his/her roll number (mapping). will the change be also seen when Array[0](say the student is in that index when pushed) is accessed?

1 Answer 1

0

The short answer is NO.

We use different objects: mapper as HashMap and Array as array. They are not related in this example

3
  • But according to different programming language since struct is a reference type, why fetching and modifying the value of fields that in within a struct referenced by multiple type in different wouldn't affect the value? Please help me since I am very beginner to this :). Thanks in advance.
    – Niladri
    Feb 3 at 14:14
  • Even in another language the result will be the same - you have created two different objects and they court independent of each other Feb 3 at 18:42
  • It's just that these objects have a struct abc{...} as part of them Feb 3 at 18:44

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.