Exploring Data Structures in Solidity for Advanced Smart Contracts

6 min read

Smart contracts are like self-executing agreements running on blockchain technology. They offer a safe and transparent method of managing digital assets and automating processes. Data structures such as structs, arrays, and maps are frequently used by developers to design more complicated smart contracts.

In this blog post, we’ll explore these data structures and understand how they can be useful in smart contract development in simple terms. Let’s start with the array.

Array

An array is a homogeneous data structure that stores a fixed collection of elements of the same data type. Each element in the array has a specific location called an index. Arrays are used to store and organize data, making it easier to retrieve and modify variable collections.

Instead of declaring individual array variables like num0,num1, num 2, etc you can declare a single array variable, such as numbers. By using indexing, you can refer to specific elements within the array, such as numb[0], num[1], and num[100], to represent particular variables.

Arrays can be of two types in solidity — compile-time fixed size array or dynamic size array. Different types of elements can be stored in storage arrays. However, in the case of memory arrays, the element type cannot be a mapping. If an array is used as a function parameter, the element type should be an ABI type.

An array’s elements are all stored in contiguous memory regions. The first element represents the lowest address, while the final is the highest address.

Declaring Arrays

There are three types of arrays in Solidity:

1. Fixed-Size Arrays

Fixed-size arrays have a predetermined length that cannot be changed once defined.

The syntax for declaring a fixed-size array is as follows:

dataType[size] arrayName;

Let’s take an example:

uint[10] public fixedArray;

In the above example, we declare a fixed-size array named fixedArray of type uint with a length of 10. The public keyword permits access to an array from outside the contract.

2. Dynamic Arrays

Dynamic arrays have a variable length that can be changed during execution.

Here is the syntax for declaring a dynamic array is as follows:

dataType[] arrayName;

Let’s take an example:

uint[] public dynamicArray;

In the given example, we declare a dynamic array named dynamicArray of type uint. It can hold an arbitrary number of elements which will be dynamic.

Array Initialization

Arrays can be initialized in solidity at the time of declaration using an initializer list.

Here is the syntax for initializing an array is as follows:

dataType[] arrayName = [element1, element2, …];

Example:

uint[] public initializedArray = [10, 20, 30];

Accessing Array Elements

An element is accessed in solidity by indexing the array name. The elements of the array are accessed by using the index.

For example:

uint price = book[2];

The above-given statement will take 3rd element from the array and assign the value to the price variable.

The Drawback of Arrays

Memory deletion in Solidity is a limitation as memory arrays are temporary and get cleared after function execution, potentially resulting in data loss and unexpected behavior.

Suggested Read | Why Use Solidity for Smart Contracts Development

Struct

There are different basic data types available in Solidity such as uint(unsigned Integers, bool, array, and string. But as a blockchain developer, you may need a flexible data type that you can define by yourself. In Solidity, a struct is a data structure format in which variables of various data types can be bundled into a single variable or a custom-made type. Once the data types have been gathered into a struct data type, the struct name symbolises the variable subsets included within it.

Defining a Struct

To define a Struct, you must have to use the struct keyword. The struct keyword defines a user-defined data type, with more than one member. The format of the struct statement is given as follows:

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

Example:

struct student{

string name;

uint roll_no;

string class;

}

Accessing a Struct and its Variable

To access a struct’s variables, we add a dot followed by the variable name on a struct object. To get Alice’s balance, we just need to call Alicealance, and to get Alice’s address, we just need to call Alice.userAddress.

function getUserAddress()public view returns(address){

return alice.userAddress;

}

Check It Out | Why Choose Solidity for Creating Ethereum Smart Contracts

Mappings

Mapping is identical to a dictionary in every other language. These are used to store the data in the form of key-value pairs, a key can be any of the built-in data types but reference types are not allowed while the value can be of any type. Mappings are extensively used in Solidity and the Ethereum blockchain to relate a unique Ethereum address to provided equivalent values.

Syntax to Declare a Mapping Type

mapping(_KeyType => _ValueType)

Note:

  • KeyType — It can be any built-in type plus bytes and string. Complex objects and reference types are not allowed.
  • ValueType — It can be any type.

Also, Explore | A Definitive Guide to Smart Contract Development Tools

Takeaways

  • Mappings act like hash tables which are used to consist of key-value pairs
  • Mappings are useful because they can store many _KeyTypes to _ValueTypes
  • Mappings do not have a length, nor do they have a concept of a key or a value being set
  • Mappings can only be used for state variables that act as storage reference types in solidity.
  • Hire smart contract developer to design, implement, and deploy Solidity smart contracts with advanced data structures.

Interested in smart contract development? Connect with our blockchain developers today!

In case you have found a mistake in the text, please send a message to the author by selecting the mistake and pressing Ctrl-Enter.
Pranjali Tiwari 4
Joined: 10 months ago
Comments (0)

    No comments yet

You must be logged in to comment.

Sign In / Sign Up