How to create a dynamic memory array in Solidity?
4,342
Try this way. This should work.
address[] memory defaultOperators = new address[](1);
defaultOperators[0] = address(this);
Related videos on Youtube
Author by
kowasaur
Updated on December 28, 2022Comments
-
kowasaur over 1 year
I'm creating an ERC777 from a contract and I'm having trouble with the default operators. My code compiles fine but when I try to deploy it it doesn't work.
Here's a simplified version of my code
contract Amacoin is ERC777 { address public amaclicker; constructor(address[] memory defaultOperators) ERC777("Amacoin", "AMAC", defaultOperators) { amaclicker = defaultOperators[0]; } } contract Amaclicker { Amacoin public amacoin; constructor() { address[] memory defaultOperators; defaultOperators[0] = address(this); amacoin = new Amacoin(defaultOperators); } }
From my testing, it's the
defaultOperators[0] = address(this);
that doesn't work. Also just putting[address(this)]
as the argument for Amacoin or defaultOperators doesn't work.Edit: I still haven't found a solution but a workaround I've done is create a state variable and then delete it after creating Amacoin
-
Patrick Collins about 3 yearsDoes this answer your question? Dynamic array in Solidity
-
-
kowasaur about 3 yearsThis doesn't work
TypeError: Member "push" is not available in address[] memory outside of storage.
-
Sudipta Basak about 3 yearsI have modified the answer. Check this one. @kowasaur
-
nosbor about 2 yearsBut actually, this is not a dynamic array. As far as I know, the dynamic array is an array that is resizable i.e. has a
push
function. The mentioned here is not resizable. It's simply a declaration of static array with one element (n-elements) inside it. It is indeed a "dynamically allocated array" but NOT a "dynamic array".