Solidity Minimalist #6. Reference Types

In this lecture, we will introduce two important variable types in solidity: array (array) and structure (struct).

array array

Array (Array) is a variable type commonly used in solidity to store a set of data (integer, byte, address, etc.). There are two types of arrays: fixed-length arrays and variable-length arrays:

  • Fixed-length array: Specify the length of the array when declaring it. Declare in the format T[k], where T is the type of the element and k is the length, for example:
// 固定长度 Array
uint[8] array1;
bytes1[5] array2;
address[100] array3;
  • Variable-length array (dynamic array): The length of the array is not specified at the time of declaration. Declare in the format T[], where T is the type of the element, for example:
// 可变长度 Array
uint[] array4;
bytes1[] array5;
address[] array6;
bytes array7;

Note : bytes is special, it is an array, but there is no need to add []. In addition, you cannot declare a single-byte array with byte[], you can use bytes or bytes1[]. In terms of gas, bytes is cheaper than bytes1[]. Because bytes1[] needs to add 31 bytes for filling in memory, additional gas will be generated. But in storage, because the memory is tightly packed, there is no byte padding.

Rules for creating arrays

In Solidity, there are some rules for creating arrays:

  • The dynamic array modified by memory can be created with the new operator, but the length must be declared, and the length cannot be changed after declaration. example:
// memory动态数组
uint[] memory array8 = new uint[](5);
bytes memory array9 = new bytes(9);
  • Array literal constants (Array Literals) are arrays in the form of expressions, a way to initialize the array with square brackets, and the type of each element in it is based on the first element, for example, all elements in [1,2,3] are uint8 type, because in solidity if a value does not specify a type, the default is the type of the smallest unit, here the default smallest unit type of int is uint8. The elements in [uint(1), 2, 3] are all uint type, because the first element is specified as uint type, we all take the first element as the standard. In the following contract, for the call in the f function, if we do not explicitly convert the first element to uint, an error will be reported, because as mentioned above, we actually passed in an array of type uint8, but what the g function needs is an array of type uint, and an error will be reported.
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.4.16 <0.9.0;

contract C {
  function f() public pure {
      g([uint(1), 2, 3]);
  }
  function g(uint[3] memory) public pure {
      // ...
  }
}
  • If you are creating a dynamic array, you need to assign values ​​one by one.
uint[] memory x = new uint[](3);
x[0] = 1;
x[1] = 3;
x[2] = 4;

array member

  • length: The array has a length member that contains the number of elements, and the length of the memory array is fixed after creation.
  • push(): Dynamic arrays and bytes have push() members, which can add a 0 element at the end of the array.
  • push(x): Dynamic arrays and bytes have push(x) members, which can add an x ​​element at the end of the array.
  • pop(): Dynamic arrays and bytes have a pop() member, which can remove the last element of the array.

Example:

Solidity minimalist entry #6. Reference type _web3

structure struct

Solidity supports defining new types by constructing structures. How to create a structure:

// 结构体
struct Student{
  uint256 id;
  uint256 score; 
}
Student student; // 初始一个student结构体

There are two ways to assign values ​​to structures:

//  给结构体赋值
// 方法1:在函数中创建一个storage的struct引用
function initStudent1() external{
  Student storage _student = student; // assign a copy of student
  _student.id = 11;
  _student.score = 100;
}

Example:

Solidity Minimalist #6. Reference Type_web3_02

// 方法2:直接引用状态变量的struct
function initStudent2() external{
  student.id = 1;
  student.score = 80;
}

Example:

Solidity Minimalist #6. Reference Type_web3_03

Summarize

In this lecture, we introduced the basic usage of array (array) and structure (struct) in solidity. In the next lecture we will introduce the hash table in solidity - mapping.

Guess you like

Origin blog.csdn.net/u010359479/article/details/128891387