Data Structures In Solidity

Data Structures In Solidity

Data Structure are the backbone for programming. Lets look at the backbone of solidity.

Β·

4 min read

You can read about basics of solidity like variables and different keywords, here πŸ‘‡

Basic of Solidity (PART 2)

Let's go,

πŸ’œ Data Structures are simply a structure in which we store the data, so that it allows certain method to be implemented. This saves time as well as in some cases memory.

πŸ’œ Every Data Structures has their trade offs and proper use. So depending on the scenario, one data structure may work better than other.

πŸ’œ ARRAYS

  • An array is a collection of items of same data type stored at contiguous memory locations.
  • This makes it easier to calculate the position of each element by simply adding an offset to a base value, i.e., the memory location of the first element of the array (generally denoted by the name of the array). The base value is index 0 and the difference between the two indexes is the offset.

FIXED ARRAYS

<DATA TYPE> <ACCESS MODIFIER> <NAME>

uint[10] public balance; // This would create size of 10 indexes, If we try to store more it would fail.

address[200] private address_arr; // This would create size of 200 indexes, If we try to store more it would fail.

STORING VALUES IN ARRAYS

balance[0] = 200; // Stores value at index 0. 
balance[1] = 203; // Stores value at index 1.

πŸ‘‰ Remember, Arrays index starts from 0. So a 10 size array will have index from 0 to 9.

DYNAMIC ARRAYS

<DATA TYPE> <ACCESS MODIFIER> <NAME>

uint[] public balance; // This would create a dynamic size array, will increase the size as per need.

address[] private address_arr;

πŸ‘‰ Remember, Its good practice to use dynamic array only when required. πŸ‘‰EVM implements some constraints to force developers to keep their code as much optimized as much possible.

ACCESSING VALUES IN ARRAYS

<DATA TYPE> <ACCESS MODIFIER> <NAME>

uint[10] public balance; // This would create a dynamic size array, will increase the size as per need.

🍏 public_balance[8]; //This would give the value stored at 8th position in the array.
β›” public_balance[10]; // This would give error as index are from 0 to 9;

address[] private address_arr;

πŸ’œ MAPPING

  • A mapping is like a dictionary, which has key and value pair.
  • You simply provides the system the key, it returns the value paired with.

MAPPING

mapping(<DATA TYPE> => <DATA TYPE>) <ACCESS MODIFIER> <NAME>

mapping(string=>uint256) public balance; // This would require a key of string and values of uint256

mapping(address=>uint256) public balance2; // This would require an address as key and values of uint256

πŸ‘‰ Remember, Keys will have to be unique else it will overwrite the value earlier stored corresponding to the key.

STORING VALUES IN MAPPING

balance["abc"] = 26;
balance2[0x.....] = 92;

ACCESSING

balance["abc"]; //This would give result as 26.
balance2[0x....]; // This would give result as 92.

πŸ’œ STRUCT

  • A Struct is used when we need to combine the use of different data variables. Like one variable stores uint, string, int etc.
  • Its like a custom variable which one can create and use as per need. These can also store functions within themselves.

STRUCT

Struct <NAME>{ <DATA TYPE> <NAME> }

Struct People {
  uint256 number;
  String name;
}

DEFINING IT

<STRUCT NAME> <ACCESS MODIFIER> <NAME> = <STRUCT NAME> ({..provide details of variable as shown...acts as a constructor})
People public people1 = People ({number:5, name:"Tanisk"});
People public people2 = People ({number:92, name:"Sharma"});

USING IT

<NAME>.<VARIABLE IN STRUCT>
people1.name;

πŸ‘‰ There are more Data Structure, but these are enough for writing smart contracts as they are used extensively. πŸ‘‰ There are few more but we can learn in future articles.

That's all.

In the next article, we will look into few nuances of EVM and how memory is handled in EVM.

Hello, I am Tanisk Annpurna

I post about

πŸš€web3, Blockchain, Ethereum

🐦Smart Contract, Solidity

πŸŽ‰JavaScript, ReactJS, NodeJS

Follow and like for more such posts. !!✌️!!

Did you find this article valuable?

Support Tanisk's Blog by becoming a sponsor. Any amount is appreciated!

Β