0

I write code of fixed size array with 6 index in array ,but array have 5 elements i need to append only 1 element, i cant use push function

// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.1 <0.9.0;
contract myArray{
    uint[6] public ipadProPrice=[1000,1100,1200,1300,1400,1500];

    function addToPrice(uint _addToPrice) public{
       ipadProPrice.push(_addToPrice);
    }

}

1 Answer 1

2

You can't push to a fixed size array because it has been fixed already, and memory has already been allocated to it. If you want to push, you need to use dynamic arrays. Dynamic arrays have no fixed size, can contain numbers, and keep growing. You can push, however, as follows:

pragma solidity >=0.7.1 <0.9.0;
contract myArray{
    uint[] public ipadProPrice =[1000,1100,1200,1300,1400,1500];

    function addToPrice(uint _addToPrice) public{
       ipadProPrice.push(_addToPrice);
    }
}

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.