My aim is to return all the family members inside of the mapping but i keep getting this error from remix Type uint256 is not implicitly convertible to expected type address. --> members[i] = familyMembers[i];

pragma solidity ^0.8.0;

contract Family{
mapping(address => uint) public familyMembers;

function getAllMembers() public pure returns (address[] memory){
  uint addressRegistryCount = 0;  
  address[] memory members= new address[](addressRegistryCount);
  for (uint i = 0; i < addressRegistryCount; i++) {
     members[i] = familyMembers[i];
  return members;
  • here you start with uint addressRegistryCount = 0; so you will always create a fixed size array of 0 size, and you're for loop will always stop at zero. you can't iterate a mapping. Take a look at this. It should be helpful youtube.com/watch?v=YOjo_lvUhj8
    – sola24
    Commented Jun 7, 2022 at 11:57

2 Answers 2


Mappings are not iterable.

In order to access one of the slots in a mapping, you need to use the key. In your case, it's the address.

Correct syntax is:

mapping(address => uint) public familyMembers;
familyMembers[someAddress] //This will return the number of family members
pragma solidity ^0.8.14;

import "hardhat/console.sol";

contract Family {

    mapping (address => string) public familyMembers;   // store data
    mapping (address => bool) public isInMap;   // need this as we can't distinguish a zero cause not in the map from is in map but is zero
    address[] public keys;  // something we can enumerate and use each element as the key for the familyMembers map

    constructor () {
        addFamilyMember(address(0x1), "alice");
        addFamilyMember(address(0x2), "bob");
        addFamilyMember(address(0x3), "cam");
        addFamilyMember(address(0x4), "doris");
        addFamilyMember(address(0x5), "");        //  <---
        addFamilyMember(address(0x5), "eve");     //  <---
        addFamilyMember(address(0x6), "fud");     //  <---
        addFamilyMember(address(0x6), "fred");    //  <---


    function addFamilyMember(address _addr, string memory _name) public {
        familyMembers[_addr] = _name;
        if(!isInMap[_addr]) {
            isInMap[_addr] = true;

    function iterateFamilyMembers() public view {
        for(uint i; i < keys.length; ++i){
            console.log("address: %s, name: %s", keys[i], familyMembers[keys[i]]);


 address: 0x0000000000000000000000000000000000000001, name: alice
 address: 0x0000000000000000000000000000000000000002, name: bob
 address: 0x0000000000000000000000000000000000000003, name: cam
 address: 0x0000000000000000000000000000000000000004, name: doris
 address: 0x0000000000000000000000000000000000000005, name: eve
 address: 0x0000000000000000000000000000000000000006, name: fred

this is a pattern for an iterable mapping. we need an array to complement the mapping, as it's iterable. we also put the address of each map element in the array so we can use that as the key to pull the values from the map as we iterate the array.

we also need another map to tell us whether an element has been added already or not. we need that as there is no null in solidity so in some cases there is no way for us to be certain whether a zero or empty string returned from a mapping is because it wasn't found, or it was found and the zero or empty string is a valid value.

In your familyMember / name example, perhaps this wouldn't be an issue, but in other cases it would.

An example of where it would be a problem is a mapping of address and account balance. A balance of zero is valid and common. When we look up the address in the mapping and get a zero, was the address in there with a zero balance or just not there? We need to know that to know whether to add another element to the array or not.

If we didn't want to make the mapping iterable, it probably wouldn't matter why we got a zero or an empty string.

  • if the answer helped you please upvote and accept
    – sola24
    Commented Jul 5, 2022 at 8:08

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.