solidity智能合约[40]-memory属性
下面是一段错误的代码,错误的原因在于,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);
}
本文链接:https://dreamerjonson.com/2018/11/23/solidity-40-memory/
版权声明:本博客所有文章除特别声明外,均采用CC BY 4.0 CN协议许可协议。转载请注明出处!
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。