The Chain logo

Exploring Data Structures in Solidity for Advanced Smart Contracts

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. To create more advanced smart contracts, developers often use data structures such as structs, arrays, and mappings.

By Pranjali TiwariPublished 10 months ago 4 min read
Like

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 organise data, making it easier to retrieve and modify variable collections.

Instead of declaring numerous array variables such as num0,num1,num2, and so on, you can specify 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 corresponds to the lowest address, while the last element corresponds to 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.

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 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 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

In solidity, an element is accessed by indexing the array name. The index is used to access the array's elements.

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. As a blockchain developer, though, you may require a flexible data type that you may define 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 are grouped into a struct data type, the struct name represents the subsets of variables present in 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 justneed 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 the concept of setting a key or a value

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!

#Smartcontractdevelopmentcompany

#Blockchainsmartcontractsdevelopment

#Smartcontractdevelopmentservices

#Smartcontractdevelopment

blockchainsmart contract
Like

About the Creator

Reader insights

Be the first to share your insights about this piece.

How does it work?

Add your insights

Comments

There are no comments for this story

Be the first to respond and start the conversation.

Sign in to comment

    Find us on social media

    Miscellaneous links

    • Explore
    • Contact
    • Privacy Policy
    • Terms of Use
    • Support

    © 2024 Creatd, Inc. All Rights Reserved.