Mapping structure before copying values

Below is the code

pragma solidity ^0.4.18;

contract Courses {
    
    struct Instructor {
        uint age;
        string fName;
        string lName;
    }
    
    mapping (address => Instructor) instructors;     /// LINE 1
    address[] public instructorAccts;
    
    function setInstructor(address _address, uint _age, string _fName, string _lName) public {
        Instructor storage instructor = instructors[_address];   /// LINE 2
        
        instructor.age = _age;          /// LINE 3
        instructor.fName = _fName;          /// LINE 4
        instructor.lName = _lName;         /// LINE 5
        
        instructorAccts.push(_address) -1;
    }
    
    function getInstructors() view public returns(address[]) {
        return instructorAccts;
    }
    
    function getInstructor(address _address) view public returns (uint, string, string) {
        return (instructors[_address].age, instructors[_address].fName, instructors[_address].lName);
    }
    
    function countInstructors() view public returns (uint) {
        return instructorAccts.length;
    }
    
}

I know that in mapping , key value is used to fetch data value. So can anyone explain how LINE 2 helps copying value into _address . Like it is var instructor = instructors[_address]; , so it means the structure stored at value _address will be copied in struct instructor of LHS. In line 3,4,5 we are copying values in struct instructor so how those values will be copied at address _address in instructors[_address] ?

1 Like

No, you've declared instructor as a storage variable, this means it's a storage pointer. The structure isn't copied. You're defining a reference to the structure in the mapping, so the assignments that follow modify it directly.