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

Bitcoin

Solana’s DEX Volume Soars, Challenging Ethereum’s Dominance ☀️💥🚀

In the past year, Solana's DEX volume market share has seen a remarkable increase, reaching an impressive 28%, while ...

Market

Vanguard CEO Shuns Bitcoin ETF, Urges Long-Term Investment The Plot Thickens!

Vanguard CEO Rejects Bitcoin ETF Support, Stresses Firm's Focus on Long-Term Investing Strategy.

Bitcoin

Vulnerability Discovered in Bitcoin Lightning Network: An Electric Shock to the System

Bitcoin technologist Antoine Riard has uncovered a potentially significant security concern within the Bitcoin Lightn...

Market

Crypto Fever Rising SEC’s Potential Approval of Bitcoin ETFs Sparks Wild Speculation and Legal Rollercoaster

The US SEC's potential approval of a Bitcoin ETF has generated excitement among cryptocurrency investors.

Bitcoin

Tech company MicroStrategy recently purchased Bitcoin worth a staggering $615M. The move proved to be a smart investment as their stock, MSTR, outperformed BTC by a ratio of 21.

Experts are considering MicroStrategy's stock to be comparable to a highly beneficial leveraged Bitcoin (BTC) ETF bec...

Blockchain

Crypto Wallet-Draining Kit, Inferno Drainer, Bids Fiery Farewell

Fashionista, the beloved Inferno Drainer is officially closing its doors after helping facilitate a staggering $80 mi...