0

I am a newbie in Solidity. Below is the snippet of code I came across :-

pragma solidity ^0.4.0; contract C {                                           //1
    function f(uint len) {                 // value passed to len = 7          //2 
        uint[] memory a = new uint[](7);                                       //3
        bytes memory b = new bytes(len);                                       //4    
// Here we have a.length == 7 and b.length == len a[6] = 8;                     // 5    
}
}

I am unable to understand why (7) is written after new uint[] in RHS of line 3. Moreover bytes is a data type of fixed length i.e bytes1 , then how did they created a new instance of it with length equal to len . At last what is meant by this b.length == len a[6] = 8 in line 5 .

1
  • uint[] memory a = new uint[](7); // This Is just another way to define fixed arrays
    – DevABDee
    Aug 13, 2022 at 7:41

1 Answer 1

0

I am unable to understand why (7) is written after new unit[]

This Is just another way to define fixed arrays.

Moreover bytes is a data type of fixed length i.e bytes1

From my experience and with the help of this article I can say that bytes can be of various lengths. And the len will be the user input.

b.length == len a[6] = 8

I think this comment is written wrong, it should've been something like this:

b.length == len,  a[6] = 8

A comma , is missing I think. If we put the comma then it makes more sense. So now it just means that b's array length will be equal to len which will be the input.

I hope you'll find this helpful

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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