3

Here is the following code:

pragma solidity ^0.4.11;
contract TestArray {
    uint[] public newOriginal;

    function TestArray(){
    }

    function copy(){
        for(uint i = 1; i<5; i++){
            newOriginal[i]=2;
        }
    }

}

Why it doesn't work? I was basically tried to copy one array to another and discovered that I can't even assign it via loop.

3

According to the docs,

Index access: If x is of type bytesI, then x[k] for 0 <= k < I returns the k th byte (read-only).

Basically, your function copy() is experiencing an off-by-one error.

Now do this:

function copy(){
    for(uint i = 0; i<5; i++){
        newOriginal[i]=2;
    }
}
  • OMG. what a shame. NO more coding after 1am. – rstormsf Jun 13 '17 at 17:34
  • I don't get it why this explanation matters. newOriginal above is not a bytesI array such as bytes32, so the index access is not read-only. However I can see the starting index is 1 which is incorrect. – Bing Ren Jan 30 '18 at 10:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.