[Ethereum] TypeError: Data location must be “memory” for return parameter public view returns (address[16]){

addressescontract-developmentsoliditytruffle

I know that is question has been answered in one form. However, I could not find an answer for my specific case.

Here is my code:

pragma solidity ^0.5.0;

contract Adoption {
 address[16] adopters;

//Adopting a Pet
function adopt(uint petId) public returns (uint){
  require(petId >= 0 && petId <= 15);
  adopters[petId] = msg.sender;
  return petId;
}

// Retrieving the adopters
function getAdopters() public view returns (address[16]){
 return adopters;


 }
 }

Right now this is throwing a

TypeError: Data location must be "memory" for return parameter in
function

but none was given error and the error is originating from

function getAdopters() public view returns (***address[16]***){

From my understanding, memory is just a key word that creates a temporary variable. Do I need to use a storage variable for the public address array?

I am going based off of this tutorial: https://truffleframework.com/tutorials/pet-shop

Best Answer

Use memory storage For return struct or array types of data is good practice and it improves the performance of code and from solidity version 0.5.0, it is compulsory to use memory storage for this type of data types.

You have to just put memory keyword in return parameter of function getAdopters() like this:

function getAdopters() public view returns (address[16] memory){

     return adopters;

} 
Related Topic