Exploring Data Structures in Solidity for Advanced Smart Contracts

Posted By : Mohika

Jun 29, 2023

Smart contracts are like self-executing agreements running on blockchain technology. They provide a secure and transparent way to manage digital assets and automate tasks. To create more advanced smart contracts, developers often use data structures such as structs, arrays, and mappings.

 

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, providing a convenient way to access and manipulate collections of variables.

 

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. For storage arrays, different types of elements can be stored. 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.

 

All elements in an array are stored in contiguous memory locations. 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 allows an array to be accessed 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

 

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. A struct is a data structure format in Solidity where variables of diverse data types can be bundled into one 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. When it is used in Solidity and the Ethereum blockchain, mappings are commonly used to link a unique Ethereum address to given corresponding 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.

 

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

Leave a

Comment

Name is required

Invalid Name

Comment is required

Recaptcha is required.

blog-detail

November 21, 2024 at 11:54 am

Your comment is awaiting moderation.

By using this site, you allow our use of cookies. For more information on the cookies we use and how to delete or block them, please read our cookie notice.

Chat with Us
Telegram Button
Youtube Button

Contact Us

Oodles | Blockchain Development Company

Name is required

Please enter a valid Name

Please enter a valid Phone Number

Please remove URL from text