12

I am creating a smart contract in solidity ^0.5.1 in which I get the following error:

data location must be a memory for the return parameter in the function, but none was given.

In the below function I am getting error.

function getCitizen()public returns(address[]){
    return citizenArray;
}

The smart contract that I have tried so far.

pragma solidity ^0.5.1;

contract Citizen{
    
    
    struct Citizens{
        
        uint age;
        string fName;
        string lName;
        
    }
    
    mapping(address => Citizens) citizenMap;
    
    address [] citizenArray;
    
    function setCitizen(address _address,uint _age,string memory _fName,string memory _lName) public{
        
        //creating the object of the structure in solidity 
         Citizens storage citizen=citizenMap[_address];
        
        
        citizen.age=_age;
        citizen.fName=_fName;
        citizen.lName=_lName;
        
        citizenArray.push(_address) -1;
        
    }
    
    function getCitizen(address _address) public pure returns(uint,string memory ,string memory ){
        return(citizenMap[_address].age,citizenMap[_address].fName,citizenMap[_address].lName);
        
    }
    
    function getCitizenAddress()public returns(address[]){
        return citizenArray;
    }
    
}

How can I return the array of addresses?

Adriaan
  • 17,741
  • 7
  • 42
  • 75
harsh
  • 337
  • 1
  • 2
  • 14

4 Answers4

39

It make sense, as you are returning the storage array of address you cannot return it as it is, because it will try to return the actual address of citizenArray in the contract storage. You can send the array by making it in memory. Like this.

function getCitizenAddress()public view returns( address  [] memory){
    return citizenArray;
}

Once you put it as memory, you will get the warning for this which will state that as you are not changing any state in the function, you should mark it view, I already did that in the above code.

Lastly, when you resolved this error, you will get another error in this function:

function getCitizen(address _address) public pure returns(uint,string memory ,string memory ){
            return(citizenMap[_address].age,citizenMap[_address].fName,citizenMap[_address].lName);
}

This error is because you mark this function as pure. There is a little but very important difference between pure and view.

  • view means you cannot change the state of the contract in that function.
  • pure means you cannot change the state in the function and not even can read the state or storage variables.

In the above function of getCitizen you are actually doing read operations in your return statement. You can fix this by just putting view instead of pure. Like So:

function getCitizen(address _address) public view returns(uint,string memory ,string memory ){
    return(citizenMap[_address].age,citizenMap[_address].fName,citizenMap[_address].lName);

}

I hope it will resolve all your issues. Thanks

Abdullah Aziz
  • 700
  • 5
  • 11
  • 1
    Do you have information on how to evaluate gas used for return array with elements? Gas used by block is limited, for really big dataset in storage we might face with issue there is not enough gas in a whole block to execute this tx. – Joe Dow Dec 14 '22 at 07:25
0

// SPDX-License-Identifier: MIT

// Version
pragma solidity >=0.8.0 < 0.9.0;

contract EstructuraDeDatos {

    struct Customer {
        string nameCustomer; 
        string idCustomer; 
        string emailCustomer;
    }

    mapping(address => Customer) public myClientes;

    address[] public listClientes;

    function registrationApp(string memory _name, string memory _id, string memory _email) public {
        Customer memory customer = Customer(_name, _id, _email);
        myClientes[msg.sender] = customer; 
        listClientes.push(msg.sender);
    }

    function retornarArrat() public view returns (address[] memory) {
        return listClientes;
    }

} 
  • Your answer could be improved by adding more information on what the code does and how it helps the OP. – Tyler2P Oct 08 '22 at 08:12
  • This function returns the array of addresses: ``` function retornarArrat() public view returns (address[] memory) { return listClientes; } ``` – Joe Dow Dec 14 '22 at 07:23
0
// SPDX-License-Identifier: GPL-3.0

pragma solidity >=0.7.0 <0.9.0;
contract Citizen {
  struct Citizens {
    uint age;
    string fName;
    string lName;
  }
    
  mapping(address => Citizens) citizenMap;
    
  address [] citizenArray;
    
  function setCitizen(address _address,uint _age,string memory _fName,string memory _lName) public {
    // Citizens storage citizen=citizenMap[_address];
    //creating the object of the structure in solidity 
    Citizens storage citizen;
    citizen = citizenMap[_address];
        
    citizen.age=_age;
    citizen.fName=_fName;
    citizen.lName=_lName;
    citizenArray.push(_address);
  }
    
  function getCitizen(address _address) public view returns(uint,string memory ,string memory) {
    return (citizenMap[_address].age,citizenMap[_address].fName,citizenMap[_address].lName);
  }
    
  // function getCitizenAddress()public returns(address[]) {
  //   return citizenArray;
  // }
}
Tyler2P
  • 2,324
  • 26
  • 22
  • 31
0
// SPDX-License-Identifier: GPL-3.0

pragma solidity >=0.7.0 <0.9.0;
contract Citizen{
    
    
    struct Citizens{
        
        uint age;
        string fName;
        string lName;
        
    }
    
    mapping(address => Citizens) citizenMap;
    
    address [] citizenArray;
    
    function setCitizen(address _address,uint _age,string memory _fName,string memory _lName) public{
        // Citizens storage citizen=citizenMap[_address];
        //creating the object of the structure in solidity 
         Citizens storage citizen;
      citizen = citizenMap[_address];
        
        citizen.age=_age;
        citizen.fName=_fName;
        citizen.lName=_lName;
        citizenArray.push(_address);

    }
    
    function getCitizen(address _address) public view returns(uint,string memory ,string memory ){
        return(citizenMap[_address].age,citizenMap[_address].fName,citizenMap[_address].lName);
        
    }
    
    function getCitizenAddress()public view returns(address[] memory){
         return citizenArray;
    }
}
0xSanson
  • 718
  • 1
  • 2
  • 11