Solidity Structs

The purpose of this article is to explore Solidity structs in depth, discussing their properties, use cases, and how to work with them effectively in smart contracts.

Using structural types, records can be represented. Let’s say you want to keep track of the books you own in a library.

It may be useful to track the following characteristics about each book:

  • Title
  • Author
  • Subject
  • Book ID


Define Struct

The struct keyword must be used to define a Struct. With the struct keyword, a new type of data can be defined that has more than one member.

Struct statements follow the following format

struct struct_name {
type1 type_name_1;
type2 type_name_2;
type3 type_name_3;
}

Example

struct Book {
string title;
string author;
string subject;
uint book_id;
}


Access Struct and Its Variable

A structure member can be accessed by applying the member access operator (.).

This operator is defined as a period between the name of the structure variable and the name of the structure member we wish to access.

Structs are used to define variables of structure types.

The following program provides the basic understanding of solidity structures:

Example: 

// SPDX-License-Identifier: GPL-3.0pragma solidity ^0.8.0;contract SolidityTest { struct Book_structure{uint book_id; string book_name; string book_subject; string book_title;}Book_structure mybook;function set_book_data() public {mybook=Book_structure(1,'Mastering Solidity','Solidity Programming','Lets Learn Solidity');}function get_book_id() public view returns(uint){return mybook.book_id;}function get_book_name() public view returns(string memory){return mybook.book_name;}function get_book_title() public view returns(string memory){return mybook.book_title;}function get_book_subject() public view returns(string memory){return mybook.book_subject;}}
<div class="spinner-border" role="status"><span class="sr-only">Loading...</span></div>

Solidity Structs

We can also create an structure for a person as done below:

Example: 

// SPDX-License-Identifier: GPL-3.0pragma solidity ^0.8.0;contract SolidityTest{struct Entrepreneur_Details{uint Entrepreneur_id; string Entrepreneur_name; uint Entrepreneur_age; string Entrepreneur_Company;}Entrepreneur_Details details;function fetch_Data() public{details=Entrepreneur_Details(1,'Elon Musk',51,'Tesla ,Inc');}function get_Start() public pure returns(string memory){return "The Following are the details Regarding the Entrepreneur : "; }function get_Id() public view returns(uint){return details.Entrepreneur_id; }function get_Name() public view returns(string memory){return details.Entrepreneur_name; }function get_Age() public view returns(uint){return details.Entrepreneur_age; }function get_Company() public view returns(string memory){return details.Entrepreneur_Company; }}
<div class="spinner-border" role="status"><span class="sr-only">Loading...</span></div>

Compile and check output yourself.

Conclusion

Solidity structs are a fundamental building block of smart contract development. With structs, you can define complex data structures that allow you to store and manipulate data in more meaningful ways.

By leveraging the power of structs, you can create more sophisticated and robust smart contracts that can stand up to the demands of real-world use cases.

So if you’re serious about building on the blockchain, learning about structs is a must.

We value your feedback.
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0

Subscribe To Our Newsletter
Enter your email to receive a weekly round-up of our best posts. Learn more!
icon

Leave a Reply

Your email address will not be published. Required fields are marked *