I have been trying to learn solidity.
Wrote a simple program as shown below
pragma solidity >=0.8.12 <0.9.0;
contract Test {
int[] staticArray = [int(1), int(2), int(3), int(4), int(5)];
function getStaticArray(int _pos) public view returns(int) {
int ret = staticArray[_pos];
return ret;
}
}
But giving following conversation error for some reason:
TypeError: Type int256 is not implicitly convertible to expected type
uint256.–> first.sol:13:31:
|
13 | int ret = staticArray[_pos];
| ^^^^
Which I am not sure why this error is thrown. Can anyone help me with it?
>Solution :
You have to use uint instead of int when you are using index in an array.
function getStaticArray(uint _pos) public view returns(int) {
int ret = staticArray[_pos];
return ret;
}