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; } }
- Is it enough for me to take 100 bitcoins to fry shoes?
- Detonating "under the chain", Celer Network will attend the 2019 than the original global developer conference
- State Grid E-commerce Company established blockchain subsidiary to develop "blockchain + power"
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; } }
- 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.
- 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!
Was this article helpful?
93 out of 132 found this helpful
Related articles
- Blockchain welcomes "the first year of landing" Supply chain finance into the mainstream scene
- Opinion: DeFi brings Ethereum to the bottom of the financial market, but as a service provider, it is going backwards.
- Cocos-BCX first launched on the first day of the currency "A wave of violent"? The reason may be here
- Cycle observation | market, economy and cycle (on)
- How to scientifically measure the distribution of blockchain assets such as Bitcoin?
- The four families of the Italian Mafia have made a "criminal chain", which clearly divides the rights of the family…
- Comprehensive interpretation of central bank digital currency: from operating system, management model to technical route