Index

Solidity

  1. types
  2. External
  3. Library
    1. self in Library
    2. using * for
    3. self example
  4. interfaces
  5. import
  6. msg.value
  7. Handling ether
  8. Erc20 token in contract
  9. assembly
  10. Enum
  11. memory, storage, calldata
  12. Array Pushing
  13. length in array
  14. Struct array
  15. mapping
  16. bytes and string
  17. uint to bytes32
  18. returns with name
  19. score voting
  20. Tests
    1. Async Tests
    2. Hooks

Array Pushing

pragma solidity >=0.4.22 <0.7.0;


contract ArrayPushing {

  uint[] myarray = new uint[](0);
  function pushTheArray (uint len) public {
       myarray.push(len);
    }
  function getTheArray() public view returns (uint[] memory) {
      return myarray;
  }

}




images/802-1.png

getTheArray
Output:
0: uint256[]:
See the output is empty

pushTheArray(5)

getTheArray
output:
0: uint256[]: 5

pushTheArray(10)

getTheArray
0: uint256[]: 5,10

Twisted code:
pragma solidity >=0.4.22 <0.7.0;


contract ArrayPushing {

  uint[] myarray = new uint[](5);
  function pushTheArray (uint len) public {
       myarray.push(len);
    }
    
  function addElementToIndex(uint index, uint data) public {
      myarray[index] = data;
  }
  
  
  function getTheArray() public view returns (uint[] memory) {
      return myarray;
  }

}


images/802-2.png

getTheArray
output:
0: uint256[]: 0,0,0,0,0
See it has 5 elements

pushTheArray(5)

getTheArray
output:
0: uint256[]: 0,0,0,0,0,5
Now it has 6 elements


addElementToIndex(5,22)

getTheArray
output:
0: uint256[]: 0,0,0,0,0,22

addElementToIndex(1, 101)

getTheArray
output:
0: uint256[]: 0,101,0,0,0,22

addElementToIndex(10, 101010)
Result:
invalid opcode