Search⌘ K
AI Features

Structs

Explore how to create and manage structs in Solidity, enabling you to group multiple data types into custom composite types for better data organization in smart contracts. Understand declaration, access with dot operator, and practical use cases within a contract.

We'll cover the following...

In Solidity, we can create custom data types that can have various properties by using structures, also called structs. By designing a struct, we create a customized data type that allows us to bundle data together into a single category. We can even import these structures from one contract into another, provided that they’ve been declared externally. This is particularly useful because it allows us to use structs as a way to represent records or complex entities within our smart contracts.

Declaration

The struct keyword is used to define a composite data type that groups variables under a single name. These variables can be of different data types, and they’re known as members of the structure. The syntax for defining a struct is as follows:

struct <structure_name> {
<data type> variable_name;
<data type> variable_name;
}
The syntax for declaring a struct in Solidity
    ...