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

Policy

Babbitt Column | US Prosecution Investigative Exchange and Precautions

(For more details, see also Deng Jianpeng, Sun Penglei: “Intermediary Chain Supervision and Compliance Respons...

Blockchain

Market Weekly | The market is in a consolidation period, and the exchange has picked up

Weekly summary Last week, the average daily market value of global digital currency assets was 326.973 billion US dol...

Blockchain

Zhongying Internet publicly claimed that it is preparing for the first of the A-share listed companies in the digital currency trading platform.

This article Source: Finance Network · Chain Finance , the original title "Save capital chain break risk A-...

Opinion

Unveiling the FTX Empire's 'Second-in-Command' The Glorious and Falling Journey of Chinese Genius Programmer Gary Wang

What has Gary Wang gone through, from being a close friend of SBF to becoming the COO of FTX and a key witness?

Blockchain

Deep Dive into Uniswap V4: A "Masterpiece" of Decentralized Exchange

Updates to Uniswap V4 could further enhance its position in the DEX space, with the "hooks" feature greatly improving...

Blockchain

Bibox and SKR staged the coin ring, and the IEO gambling nature became more intense.

At 8 am on the 22nd, two hours before the start of the first Star Project (IEO) on the Bibox Exchange, Bibox official...