Solidity basic knowledge of Basics (iv) Enumerations, storage types, and arrays

Source: Internet
Author: User
Tags array length arrays data structures

Today Mitchell salutes Carter turned to succeed in the slam Dunk King, but only the shape and no God, even shape almost mean--.


Enumeration Type: the possible values of a variable are known, it can be defined as an enumeration type, and then the value of the variable one by one is listed, so that the value of the variable is limited to the list of values within the range. For example, month, day of the week, weather, and so on, enumeration types are defined with the enum keyword.

Example: Enum Weekday{sun,mon,tue,wed,the,fri,sat};

The above declares an enumeration type weekday, Sun mon in curly braces, etc. called an enumeration element, and the value of weekday can only be seven values in parentheses.

The value of an enumeration type can be referenced in a program using the Weekday.sun format. The value of an enumeration element is not specified, the first default is 0, and the subsequent increment one, such as sun=0,mon=1,tue=2. The specified value is used when the value is specified, and then the element is increased by one if it is not specified.


Data location (storage type): Complex types, such as arrays (arrays) and data structures (structs) have an additional attribute in solidity, where data is stored in the same way that data is stored, temporarily or permanently. Optional memory (temporary) and storage (permanent).

Memory storage location is consistent with our normal program. That is, the allocation, that is, the use, beyond the scope is not accessible, waiting to be recycled. In the blockchain, because the bottom layer realizes Turing complete, so there will be a lot of states need to be recorded permanently. For example, all participants involved in crowdfunding, all who participated in the poll. So we're going to use this type of storage, and once this type is used, the data will always be there.

Based on the context of the program, most of the time such choices are default, and we can modify it by specifying the keyword storage and memory.

The default function parameters, including the returned parameters, are memory. The default local variables are storage. The default state variable (the public variable of the contract Declaration) is storage.

There's also a third storage location, Calldata. It stores a function parameter, is read-only, and does not permanently store a data location. Parameters of external functions (not including return parameters) are forced to be specified as Calldata. The effect is almost the same as memory.

Data location designation is important because different data location variable assignments produce different results. A completely unrelated copy is always created between memory and storage, as well as between them and the state variables, even if they are assigned to each other from another state variable. (This sentence did not understand, then the discussion.) )

Assigning a storage state variable to a storage local variable is passed by reference. So for the modification of local variables, the associated state variables are also modified. On the other hand, assigning a memory reference type to another memory reference does not create another copy. (Re-discussion, ——。 )


Array (arrays): An array of type uint with an element number of 5 should be defined like this: uint[5] x, and an array with a variable number of elements should be defined as: uint[] X. Defines a multidimensional array: UINT [5][4] x, which represents a two-dimensional array x containing four arrays, each with five elements. uint[][4], which represents four arrays, with variable number of elements in each array. It is important to note that the number of elements in a multidimensional array declaration is reversed in languages other than Python. X[2][1], which means accessing the second element in the third array, where the order is in the normal order (--. )。

A type is an array of state variables that can be marked as public (common), allowing solidity to create an accessor (again).

The array has a length property that represents the current array length (that is, the number of elements in a one-dimensional array, and the number of one-dimensional arrays in a two-dimensional array). A variable-length array of type storage can change the length of an array by assigning a value to length, but an array of memory types (such as an array created with the New keyword) does not support this function, but can be changed by adjusting the parameters.

Example:

Contract C {
    function f () {
        //Create an array of memory
        uint[] Memory A = new uint[] (7);
        
        Cannot modify the length of//error:expression have to is an
        lvalue.
        A.length = +;
    }
    
    Storage
    uint[] b;
    
    function g () {
        b = new uint[] (7);
        An array of storage can be modified
        b.length = ten;
        B[9] = +;
    }
}

As you can see, the storage type can be changed by the length of the value, but not by the memory type.

The Push method: The variable-length array of storage and the bytes array have a push method for adding new elements to the end of the array, and the return value is the new length.

Example: a=[1,2,3] A.push (4) means to add element 4 to array A, the new A is [1,2,3,4], and returns the new length of array A.






Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.