0

Huge newb here. I want to create multiple arrays that are fixed size arrays of two addresses, but I want to be able to append to the fixed size array from seperate addresses at different times, i.e. after I've switched accounts in Metamask. So from the UI I would click on the approve function that would add msg.sender to the fixed size array, and then once I switched accounts in metamask and clicked on the approve function again, it would add the new msg.sender to the array, giving me a list of arrays that looks like:

0: [msg.sender(from first appprover), msg.sender(from second approver)]
1: [msg.sender, msg.sender]

My current code looks like:

struct Approver {
    uint id;
    address approver;
}

Approver[] public approvers;

And the approve function would look like:

function approveTransfer() external {
    ...
    approver.push(Approver(approver.length, msg.sender));
}

function ApproverList() external view returns(Approver[] memory) {
    return approver;
}

This returns a list of arrays with one address, but I'm trying to get the two. Any help would be greatly appreciated!

1
  • I want to be able to append to the fixed size array - that's an oxymoron. If it's fixed size, then you cannot append to it! – goodvibration Dec 21 '20 at 6:59
1

You cannot append or push to fixed size arrays because they are already fixed. the memory is alocated to them. see the example bellow.

function memoryArray() external pure{
    uint[] memory newArray = new uint[](5);
    // push is not supported
    newArray[0] = 2;
}

you are pushing approver, which is an address. How ever, approvers is a dynamic array and you can push into it. Making the Approver Array public will automatically makes you a get function. you donn't need a ApproverList() function for that.

how ever you can define the ApproverListfunction like this.

function ApproverList() external view returns(Approver[] memory) {
return approvers;
}  

note that you need to fix this line as well

approver.push(Approver(approvers.length, msg.sender));

you are basically assuming that approver (address) is an array.

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.