'How to pass array of struct of type voter in solidity?

I want to pass an array of struct to a function. The array is of type voter. So below is my code. But I am getting error as

UnimplementedFeatureError: Copying of type struct Election.voter memory[] memory to storage not yet supported.

struct voter{
    string name;
    address constituency;
    uint age;
    bool isVoted;
}

struct constituency{
    string name;
    voter[] candidates;
}

constituency[] public constituencyRegister;
function addConstituency(string memory _name,voter[] memory _candidates)
    public
{
    constituency memory c = constituency(_name, _candidates);
    constituencyRegister.push(c);
}


Solution 1:[1]

In the following line you are trying to assign a memory array of struct to a storage array of struct, since c contains _candidates:

constituencyRegister.push(c);

Instead, you could push to a storage var, get its reference and only then assigning the name and pushing each _candidates element to the array:

pragma solidity ^0.8.0;

contract MuC {
    struct voter{
        string name;
        address constituency;
        uint age;
        bool isVoted;
    }

    struct constituency{
        string name;
        voter[] candidates;
    }

    constituency[] public constituencyRegister;
    function addConstituency(string memory _name,voter[] memory _candidates)
        public
    {
        constituency storage c = constituencyRegister.push();
        c.name = _name;
        for(uint i = 0; i < _candidates.length; i++) {
            c.candidates.push(_candidates[i]);
        }
    }
}

This is compiling for me with 0.8.9.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Matheus