结构如何使用一个空数组,后来将如何放置地址?
据我所知,新地址[](number)
创建了以零初始化的数字的固定尺寸数组。但是如何在此行中房间内存室=房间(新地址[](0),0,0);
room struct struct at占空数阵列,以后如何地址要放在那里吗?
pragma solidity ^0.4.18;
contract StructArrayInit {
event OnCreateRoom(address indexed _from, uint256 _value);
struct Room {
address[] players;
uint256 whosTurnId;
uint256 roomState;
}
Room[] public rooms;
function createRoom() public {
Room memory room = Room(new address[](0), 0, 0);
rooms.push(room);
rooms[rooms.length-1].players.push(msg.sender);
OnCreateRoom(msg.sender, 0);
}
function getRoomPlayers(uint i) public view returns (address[]){
return rooms[i].players;
}
}
As far I know, new address[](number)
creates an fixed sized array of that number initialized with zero. But how in this line Room memory room = Room(new address[](0), 0, 0);
Room struct is taking an empty array and later how an address is going to be put there?
pragma solidity ^0.4.18;
contract StructArrayInit {
event OnCreateRoom(address indexed _from, uint256 _value);
struct Room {
address[] players;
uint256 whosTurnId;
uint256 roomState;
}
Room[] public rooms;
function createRoom() public {
Room memory room = Room(new address[](0), 0, 0);
rooms.push(room);
rooms[rooms.length-1].players.push(msg.sender);
OnCreateRoom(msg.sender, 0);
}
function getRoomPlayers(uint i) public view returns (address[]){
return rooms[i].players;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在此语句中:
在第一行中,他宣布了一个带有以下值的房间结构变量:0,0和0。
这意味着
播放器
,whosturnId
,roomstate
struct变量的值为零。在第二行的情况下,他在
Rooms
数组,ROOM
struct变量之前推动。使用此语句:
他取了数组的最后一个元素(他输入的内容),然后推入
players
array room struct structmsg.sender
价值。如果要在
players
数组中插入更多地址,则可以使用以下语句:In this statement:
In the first line, he declared a Room struct variable with the following value: 0,0 and 0.
This means that
players
,whosTurnId
,roomState
struct variable have zero values.With the second line, he pushed inside
rooms
array,Room
struct variable created previously.With this statement:
he took the last element of array (what he has entered) then push inside
players
array insideRoom
struct themsg.sender
value.If you want to insert more address inside
players
array, you can use this statement: