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

Lose user trust? "Black Thursday" has reduced BitMEX bitcoin holdings by nearly 40%

This article Source: Cointelegraph Chinese , Author: MICHAEL KAPILKOV, the original title "from the black since ...

Blockchain

Bella Fang: The exchange is at the top of the food chain. How can small and medium-sized projects seize this channel?

On the afternoon of the 9th, at the 2nd Global Blockchain Summit·Wuzhen site hosted by Babbitt, Bella Fang, foun...

Blockchain

The consensus of using "money" to forge coins - a high-tech that condenses developers' miners' exchanges and users

In 1776, the American Revolutionary War broke out. Why is this war going to fight? The American side said that "...

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...

Market

Solana’s Spectacular Comeback: Moons and Stumbles

In 2023, the token has increased by over four times its starting value of $10, making it a lucrative investment for F...

Blockchain

Beginner's Guide | Why choose a highly liquid exchange?

Source: Medium Translation: First Class (First.VIP) Liquidity is the most important concept that everyone needs to un...