Solidity programming language: structure struct

Data structures can be customized using structure structs. The structure can contain all data types except itself, and if it contains itself, it will form recursion.

Definition and initialization

pragma solidity >=0.4.0 <0.6.0; contract EgStruct { mapping (string => Book) bookList; struct Book{ string name; uint pages; string[] contents; } struct Reader{ mapping (string => Book) books; string name; } Reader r; function init() public{ string[] memory b1c = new string[](3); b1c[0] = "1.0.1"; b1c[1] = "1.0.2"; b1c[2] = "2.0.1"; Book memory b1 = Book("book1",10,b1c); Book memory b2 = Book({ name:"book2", pages:20, contents:b1c }); Reader memory r1 = Reader("jack"); Reader memory r2 = Reader({ name:"rosh" }); // Reader memory r2 = Reader(bookList,"jack"); // Reader memory r3 = Reader({ // books:bookList, // name:"rosh" // }); r = r2; r.books["book1"] = b1; //r2.bookList["book2"] = b2; //r2.books = bookList; //r.books = bookList; } } 

pragma solidity >=0.4.0 <0.6.0; contract EgStruct { mapping (string => Book) bookList; struct Book{ string name; uint pages; string[] contents; } struct Reader{ mapping (string => Book) books; string name; } Reader r; function init() public{ string[] memory b1c = new string[](3); b1c[0] = "1.0.1"; b1c[1] = "1.0.2"; b1c[2] = "2.0.1"; Book memory b1 = Book("book1",10,b1c); Book memory b2 = Book({ name:"book2", pages:20, contents:b1c }); Reader memory r1 = Reader("jack"); Reader memory r2 = Reader({ name:"rosh" }); // Reader memory r2 = Reader(bookList,"jack"); // Reader memory r3 = Reader({ // books:bookList, // name:"rosh" // }); r = r2; r.books["book1"] = b1; //r2.bookList["book2"] = b2; //r2.books = bookList; //r.books = bookList; } }

pragma solidity >=0.4.0 <0.6.0; contract EgStruct { mapping (string => Book) bookList; struct Book{ string name; uint pages; string[] contents; } struct Reader{ mapping (string => Book) books; string name; } Reader r; function init() public{ string[] memory b1c = new string[](3); b1c[0] = "1.0.1"; b1c[1] = "1.0.2"; b1c[2] = "2.0.1"; Book memory b1 = Book("book1",10,b1c); Book memory b2 = Book({ name:"book2", pages:20, contents:b1c }); Reader memory r1 = Reader("jack"); Reader memory r2 = Reader({ name:"rosh" }); // Reader memory r2 = Reader(bookList,"jack"); // Reader memory r3 = Reader({ // books:bookList, // name:"rosh" // }); r = r2; r.books["book1"] = b1; //r2.bookList["book2"] = b2; //r2.books = bookList; //r.books = bookList; } }

pragma solidity >=0.4.0 <0.6.0; contract EgStruct { mapping (string => Book) bookList; struct Book{ string name; uint pages; string[] contents; } struct Reader{ mapping (string => Book) books; string name; } Reader r; function init() public{ string[] memory b1c = new string[](3); b1c[0] = "1.0.1"; b1c[1] = "1.0.2"; b1c[2] = "2.0.1"; Book memory b1 = Book("book1",10,b1c); Book memory b2 = Book({ name:"book2", pages:20, contents:b1c }); Reader memory r1 = Reader("jack"); Reader memory r2 = Reader({ name:"rosh" }); // Reader memory r2 = Reader(bookList,"jack"); // Reader memory r3 = Reader({ // books:bookList, // name:"rosh" // }); r = r2; r.books["book1"] = b1; //r2.bookList["book2"] = b2; //r2.books = bookList; //r.books = bookList; } }

pragma solidity >=0.4.0 <0.6.0; contract EgStruct { mapping (string => Book) bookList; struct Book{ string name; uint pages; string[] contents; } struct Reader{ mapping (string => Book) books; string name; } Reader r; function init() public{ string[] memory b1c = new string[](3); b1c[0] = "1.0.1"; b1c[1] = "1.0.2"; b1c[2] = "2.0.1"; Book memory b1 = Book("book1",10,b1c); Book memory b2 = Book({ name:"book2", pages:20, contents:b1c }); Reader memory r1 = Reader("jack"); Reader memory r2 = Reader({ name:"rosh" }); // Reader memory r2 = Reader(bookList,"jack"); // Reader memory r3 = Reader({ // books:bookList, // name:"rosh" // }); r = r2; r.books["book1"] = b1; //r2.bookList["book2"] = b2; //r2.books = bookList; //r.books = bookList; } }

pragma solidity >=0.4.0 <0.6.0; contract EgStruct { mapping (string => Book) bookList; struct Book{ string name; uint pages; string[] contents; } struct Reader{ mapping (string => Book) books; string name; } Reader r; function init() public{ string[] memory b1c = new string[](3); b1c[0] = "1.0.1"; b1c[1] = "1.0.2"; b1c[2] = "2.0.1"; Book memory b1 = Book("book1",10,b1c); Book memory b2 = Book({ name:"book2", pages:20, contents:b1c }); Reader memory r1 = Reader("jack"); Reader memory r2 = Reader({ name:"rosh" }); // Reader memory r2 = Reader(bookList,"jack"); // Reader memory r3 = Reader({ // books:bookList, // name:"rosh" // }); r = r2; r.books["book1"] = b1; //r2.bookList["book2"] = b2; //r2.books = bookList; //r.books = bookList; } }

pragma solidity >=0.4.0 <0.6.0; contract EgStruct { mapping (string => Book) bookList; struct Book{ string name; uint pages; string[] contents; } struct Reader{ mapping (string => Book) books; string name; } Reader r; function init() public{ string[] memory b1c = new string[](3); b1c[0] = "1.0.1"; b1c[1] = "1.0.2"; b1c[2] = "2.0.1"; Book memory b1 = Book("book1",10,b1c); Book memory b2 = Book({ name:"book2", pages:20, contents:b1c }); Reader memory r1 = Reader("jack"); Reader memory r2 = Reader({ name:"rosh" }); // Reader memory r2 = Reader(bookList,"jack"); // Reader memory r3 = Reader({ // books:bookList, // name:"rosh" // }); r = r2; r.books["book1"] = b1; //r2.bookList["book2"] = b2; //r2.books = bookList; //r.books = bookList; } }

pragma solidity >=0.4.0 <0.6.0; contract EgStruct { mapping (string => Book) bookList; struct Book{ string name; uint pages; string[] contents; } struct Reader{ mapping (string => Book) books; string name; } Reader r; function init() public{ string[] memory b1c = new string[](3); b1c[0] = "1.0.1"; b1c[1] = "1.0.2"; b1c[2] = "2.0.1"; Book memory b1 = Book("book1",10,b1c); Book memory b2 = Book({ name:"book2", pages:20, contents:b1c }); Reader memory r1 = Reader("jack"); Reader memory r2 = Reader({ name:"rosh" }); // Reader memory r2 = Reader(bookList,"jack"); // Reader memory r3 = Reader({ // books:bookList, // name:"rosh" // }); r = r2; r.books["book1"] = b1; //r2.bookList["book2"] = b2; //r2.books = bookList; //r.books = bookList; } }

pragma solidity >=0.4.0 <0.6.0; contract EgStruct { mapping (string => Book) bookList; struct Book{ string name; uint pages; string[] contents; } struct Reader{ mapping (string => Book) books; string name; } Reader r; function init() public{ string[] memory b1c = new string[](3); b1c[0] = "1.0.1"; b1c[1] = "1.0.2"; b1c[2] = "2.0.1"; Book memory b1 = Book("book1",10,b1c); Book memory b2 = Book({ name:"book2", pages:20, contents:b1c }); Reader memory r1 = Reader("jack"); Reader memory r2 = Reader({ name:"rosh" }); // Reader memory r2 = Reader(bookList,"jack"); // Reader memory r3 = Reader({ // books:bookList, // name:"rosh" // }); r = r2; r.books["book1"] = b1; //r2.bookList["book2"] = b2; //r2.books = bookList; //r.books = bookList; } }

pragma solidity >=0.4.0 <0.6.0; contract EgStruct { mapping (string => Book) bookList; struct Book{ string name; uint pages; string[] contents; } struct Reader{ mapping (string => Book) books; string name; } Reader r; function init() public{ string[] memory b1c = new string[](3); b1c[0] = "1.0.1"; b1c[1] = "1.0.2"; b1c[2] = "2.0.1"; Book memory b1 = Book("book1",10,b1c); Book memory b2 = Book({ name:"book2", pages:20, contents:b1c }); Reader memory r1 = Reader("jack"); Reader memory r2 = Reader({ name:"rosh" }); // Reader memory r2 = Reader(bookList,"jack"); // Reader memory r3 = Reader({ // books:bookList, // name:"rosh" // }); r = r2; r.books["book1"] = b1; //r2.bookList["book2"] = b2; //r2.books = bookList; //r.books = bookList; } }

pragma solidity >=0.4.0 <0.6.0; contract EgStruct { mapping (string => Book) bookList; struct Book{ string name; uint pages; string[] contents; } struct Reader{ mapping (string => Book) books; string name; } Reader r; function init() public{ string[] memory b1c = new string[](3); b1c[0] = "1.0.1"; b1c[1] = "1.0.2"; b1c[2] = "2.0.1"; Book memory b1 = Book("book1",10,b1c); Book memory b2 = Book({ name:"book2", pages:20, contents:b1c }); Reader memory r1 = Reader("jack"); Reader memory r2 = Reader({ name:"rosh" }); // Reader memory r2 = Reader(bookList,"jack"); // Reader memory r3 = Reader({ // books:bookList, // name:"rosh" // }); r = r2; r.books["book1"] = b1; //r2.bookList["book2"] = b2; //r2.books = bookList; //r.books = bookList; } }

  1. Structures in Solidity are only available in current contract or subclass contracts, external is invisible, and functions that use structs need to be decorated as internal.
  2. If you want to do this, you can pass the basic type of data through the function call, and then assemble it into the same structure array parameter again. Here you have a problem that the function parameter cannot be an unfixed complex type parameter, in the following code. Seal1 cannot be compiled.
 pragma solidity >=0.4.0 <0.6.0; 

Contract EgStruct {
Struct Book{
String name;
Uint pages;
}

Function read(Book memory b) internal{}

Function callTmp(EgStructTmp tmp) public{
Book memory b = Book("jingpinmei",10);
Tmp.seal(b.name,b.pages);
}

}

Contract EgStructSub is EgStruct{
Function write(Book memory b) internal{}
}

Contract EgStructOut {
/*function seal(Book memory b) {}*/
}

Contract EgStructTmp{
Struct Book{
String name;
Uint pages;
}
Function seal(string memory name, uint pages) public {
Book memory b = Book(name, pages);
}
}

Author: thank HPB Blue Lotus team finishing feeds.

Wang Xiaoming blog: http://wangxiaoming.com/

Wang Xiaoming: Founder of HPB core chain, Babbitt columnist. More than ten years of experience in financial big data and blockchain technology development, he has participated in the creation of UnionPay big data. The main creative blockchain teaching video program "Ming Shuo" more than 30 issues, compiled the "Ethernet official website document Chinese version", and as the main author wrote the "blockchain development guide", in the Chinese blockchain community with ID "blue lotus "famous.

We will continue to update Blocking; if you have any questions or suggestions, please contact us!

Share:

Was this article helpful?

93 out of 132 found this helpful

Discover more

Blockchain

Clear out while the time is right? FTX and Alameda-related addresses recently transferred $30 million worth of assets.

In September, FTX was approved for liquidation and has been frequently withdrawing large amounts of assets in the pas...

Opinion

Tokyo and Kyoto, the rising encrypted 'twin stars

In an era where technological advancements are shaping the future of economies around the world, Japan is taking a st...

Blockchain

"Gemini" Risk Control Interview: How the Traditional Financial Regulatory Framework Maps to the Crypto Industry

Source: Yahoo Fiance Original author: Daniel Kuhn Translator: Moni Source: Odaily Planet Daily Monero’s Community Cro...

Blockchain

Hong Kong Cryptocurrency New Policy's One-Year Anniversary A Year of Major Leaps and Key Milestone Review

Over the past year, Hong Kong has made great progress and shown strategic development in virtual asset policies. Sinc...

News

Visit: What is behind the brush of the digital currency exchange?

Recently, TokenInsight released the blockchain industry report "Exchange Real Estate Volume Report (I)" poi...

Policy

The Crypto Circus: A Bug’s Billion-Dollar Bonanza

During the 10th day of Sam Bankman-Fried's trial, talks focused on a software glitch and the allocation of funds for ...