Cover photo

6. 引用类型, array, struct-Solidity入门笔记

数组

固定数组

uint[3] nums;//声明时指定长度 

可变数组

uint[] nums;//声明时不用指定长度
//使用时,使用new并指定长度
nums = new uint[3]

数组成员

length(),push(x), pop()

数组字面常数

[1,3,4]以第一个元素的类型准,默认uint8。使用强制转换指定第一个元素的类型。

例如[(uint)1, 2, 3]

结构体

//创建结构体
struct Student{
  string name;
  uint score;
}
//声明变量
Student student

赋值

//使用storage在函数中赋值
function setStudent() {
  Student storage x = student;
  x.name = "mark";
  x.score = 90;  
}
//直接使用点号引用赋值
student.name = "alice";
student.score = 89;