0
pragma solidity ^0.5.12;
contract sample {
 mapping(address => uint256[12]) fruits;
 function getfruit(address addr) public view returns (uint256[] storage fr) { //line 3
  return (fruits[addr]);
 }
}

Look at the above code. Here firstly since fruits is public, getfruit() should not be even required. But the auto-generated fruits() only returns index 0 (only the first element) but I want all. Now as far as function is concerned, it gives error when I use 'storage' in line 3. Now if I make it memory , I tried the following code but still throws error saying 'TypeError: Type uint256[12] is not implicitly convertible to uint256[] memory' (which sounds reasonable, but then how do I get the array)?

pragma solidity ^0.5.12;
contract sample {
 mapping(address => uint256[12]) fruits;
 function getfruit(address addr) public view returns (uint256[] storage fr) { //line 3
  uint256[] memory veggies = new uint256[] (12);
  veggies = fruits[addr];
  return (veggies);
 }
}
0

Change this:

uint256[] storage fr

To this:

uint256[12] memory
| improve this answer | |
  • Still returns just the first element. – learningOneErrorAtATime Mar 10 at 13:13
  • @learningOneErrorAtATime: What do you mean "still"? In your question you've described a compilation error. How could there be "still" in here if you couldn't even compile the contract and call this function beforehand??? – goodvibration Mar 10 at 13:16
  • My bad. Tried several ways. So I was talking for the auto-generated function returning only first element. Now if we just write, function getfruit(address addr) public view returns (uint256[12] memory fr) { return (fruits[addr])} still returns first element. – learningOneErrorAtATime Mar 10 at 13:26
  • @learningOneErrorAtATime: The auto-generated function indeed returns only the first element. But you've posted your own (manually-generated) function, which I've instructed you how to fix. How exactly were you thinking to apply my suggestion on the auto-generated function??? – goodvibration Mar 10 at 13:32
  • Please show how you're calling function getfruit, and what brings you to the conclusion that it only returns 1 element of the array. – goodvibration Mar 10 at 13:33

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.