solidity智能合约[40]-memory属性

memory引入

函数中结构体变量默认是是storage类型

下面是一段错误的代码,错误的原因在于,init函数中,student s 默认会加上storage的属性,但是storage属性必须要引用storage空间中的状态变量。但是实例化的student(100,“jackson”)并不在storage中。

1
2
3
4
5
6
7
8
9
struct student{
    uint grade;
    string name;
}

function init() public pure returns(uint,string){
    student  s = student(100,"jackson");
    return (s.grade,s.name);
}

因此,正确的做法是,必须要变量的初始化放在memory空间中。加上了memory属性的变量,意味着变量存储在memory的空间中。

1
2
3
4
5
6
7
8
9
struct student{
    uint grade;
    string name;
}

function init() public pure returns(uint,string){
    student memory s = student(100,"jackson");
    return (s.grade,s.name);
}

image.png

猜你喜欢

转载自blog.51cto.com/13784902/2322318
今日推荐