0
点赞
收藏
分享

微信扫一扫

第117篇 remix 中 struct 传参

remix 中,结构体显示为 tuple,使用'[]'标识一个对象;

合约示例:

// SPDX-License-Identifier: MIT
pragma solidity 0.8.0;

contract tupleTest {

struct Man {
string name;
uint256 age;
}

Man[] persons;

constructor() {
persons.push(Man("name1",11));
persons.push(Man("name2",22));
}

// ["a1",1]
function addMan(Man memory man) public{
persons.push(man);
}

// [["a1",1],["a2",2]]
function addMen(Man[] memory men) public{
for(uint i=0;i<men.length;i++){
persons.push(men[i]);
}
}

function getMen() view public returns(Man[] memory){
return persons;
}

function getMen(uint id) view public returns(Man memory){
return persons[id];
}

}

在 remix 部署合约,并调用:

 

举报

相关推荐

0 条评论