How do Solidity structs work?
Solidity structs create custom data types for record-keeping, combining with arrays and functions to add, retrieve, and update records. They can be protected by checking msg.sender.
Letβs dive into a Solidity-specific data structure, not typically found in other programming languages like arrays areβ¦ π€¨
Structs

Up until now, weβve looked at all of the defined data types in Solidity. One super cool feature about Solidity is it allows you to define your own custom data type π€―.
You can define your own custom data type by creating a struct. Struct types are typically used to represent a record, or a grouping of common data.
The syntax to declare a struct in Solidity resembles an object declaration in JavaScript:
Super easy! π― Letβs look at a specific use case for structs to help us further understand the concept..
Struct Use Cases: Library Record Keeping π
What if we wanted to have an immutable record of all the library books we cared about? π€ Keeping our book records on a centralized server somewhere might not be ideal, since it might be taken down or corrupted by the server admin. In come smart contracts! π₯
We can create a smart contract called Library and equip it with all the functionality weβd need to perform detailed record keeping for books. For now, we can just work off of a single assumption / user story:
- For each book record I add to my Library smart contract, I want to keep track of its title, author and some sort of id for internal record-keeping
The above assumption would mean lines and lines of codeβ¦ if we didnβt have Solidity structs. π
So, letβs define a smart contract called Library and define a struct called Book inside its scope. The Book struct should keep track of all the properties we listed in the assumption above:
Great! Weβve got the base setup done. Any further functionality will be structured around this same Book struct and its properties.
A Book struct is specific to one bookβ¦ so we need a way to keep track of many of the same typeβ¦ anything ring a bell here? π Thatβs right, we can use an array. Letβs add it to our Library:
Sweet! An array here will make it super easy to keep indexed track of each Book struct, along with each bookβs specific properties.
What if we want to add a book? We can do so with a function addBook():
Ok, letβs break down the addBook() function real quick:
-
The
stringparameters usememory. This is a requirement of Solidity whenever you use reference types as a parameter, you must precede the parameter with eithermemoryorcalldata. This is just telling the Solidity compiler where the data being passed in lives. Since this is an ephemeral call, we are passing in the value frommemory. -
We use
books.push()in order to push a brand new Book struct -
We initialize the struct by calling it exactly like you would a function:
Book(_param1, param2, ...)- that is the easiest one but you can also initialize structs like so:
Ok great! We can now add new books to our library and push em to the books array for record-keeping. What if we want to retrieve a book from this record? We can add a simple getter and retrieve based on the bookId:
Awesome! Now anyone can pass in a book id and the smart contract will return that book idβs title and author! π
One final function to add before we wrap up the awesomeness of structs is update(), that way we can update a book title or author for any reason (mainly for an easy example, since we probably wouldnβt want to update such a record after itβs been sealed!).
But wait!

One more thingβ¦ notice how all of our functions are public? This means anyone could mess with our cool library and we donβt want that! Letβs make it so that each Book holds an address property, and ONLY that specific address can perform certain changes in the contract:
Letβs add a registrant of type address on the Book struct. Letβs also add an update() function:
Be careful! Since we are adding a new property to the Book struct, we will have to change any previous calls to Book to also account for it! π¨
Amazing. Weβve just coded a whole contract that has soooo much functionality in so few lines! π€― Plus, it protects our records by checking the msg.sender in the update() function! π
Suggested Reading
Conclusion
Structs are super useful in Solidity. They are a custom data typeβ¦ so they are fitting for any custom record-keeping needs you might think of! Another example would be a Player struct that keeps track of that playerβs address, level, etcβ¦
Want to see a 15-minute run-through of Structs using the same Library example as above? Hereβs Al covering Structs at DevConnect 2022 Amsterdam? π π½
Learn More About Ethereum Development
Alchemy University offers free web3 development bootcamps that explain Solidity structs and help developers master the fundamentals of web3 technology. Sign up for free, and start building today!