Get all elements from fixed array and using pop method

This is my fixed array
uint[5] public fixArray=[uint(7),8,9,10,11];

why don’t the following codes work ?

function getArr() public view returns (uint[] memory) { return fixArray; }

from solidity:
TypeError: Return argument type uint256[5] storage ref is not implicitly convertible to expected type (type of first return variable) uint256[] memory.

function pop() public { fixArray.pop(); }

TypeError: Member “pop” not found or not visible after argument-dependent lookup in uint256[5] storage ref.

1 Like

Hi @veridelisi,

Welcome to the forum!

You’re asking some good questions. Have you been experimenting with arrays after they are introduced near the beginning of the Ethereum Smart Contract Programming 101 course?

This doesn’t work because you are defining the data type of your return value as a dynamically-sized array …

… but fixArray is a fixed-sized array, and so for getArr() to be able to return it, the data type of the return value needs to be defined as …

returns(uint[5] memory)

The following code will now compile, and will successfully return the array …

function getArr() public view returns(uint[5] memory) {
    return fixArray;
}

Alternatively, if you define the data type of your variable as a dynamically-sized array e.g.

uint[] public dynamicArray = [uint(7), 8, 9, 10, 11];

… then your getArr() function, as you’ve coded it, will compile and return the array.


In Solidity, the array methods push and pop can only be called on dynamically-sized arrays. If you think about it this makes sense, because both involve modifying the size of the array, which by definition isn’t possible with a fixed-sized array. This is why your pop() function isn’t compiling. However, if you define your variable as a dynamically-sized array (as in my example above), your pop() function (exactly as you’ve coded it) will compile and each time it is called it will remove the array’s final value, reducing its length by 1.

Values cannot be removed from a fixed-sized array to the extent that this would reduce its size. However, the following function, for example, would enable values at specific index positions to be set to zero …

function setToZero(uint index) public {
    delete fixArray[index];
}

I hope this now helps you to understand the compiler errors generated by your code. Let me know if anything is still unclear, or if you have any further questions :slight_smile:

Dear Jon

Thank you for your kindly response

1 Like