In the previous post, we covered the types of functions and how to use them in Solidity. Now, let’s move on to an important aspect of Solidity: data structures, specifically arrays and structs. Both of these are essential tools when managing and organizing data in our smart contracts. In this post, we’ll continue with our simple bank setup example to explain how to use these data structures.
We’ll keep building on the simple contract from the last post and use examples related to bank accounts to make the concepts easy to understand.
What is an Array in Solidity?
An array is a collection of variables that are stored in a single variable. This is similar to what we use ain any programming language. In Solidity, we can have both fixed-size and dynamic-size arrays.
Here’s how we can define a dynamic array of unsigned integers, which could represent account balances:
uint256[] public accountBalances;
This creates a dynamic array where we can store balances for different accounts.
Accessing Array Elements (Zeroth Index)
To access an array’s first element, we simply refer to it using its index, like this:
uint256 firstBalance = accountBalances[0];
Here, we access the balance of the first account (zeroth index). Now we can use a array to access account balances but the balances are not linked with any user account like in real world banking apps.
Linking Account Balances to Users
In real world applications, balances would typically be linked to specific users, not just an list/array. This is where structs come in.
What is a Struct?
A struct allows us to create a custom data type. For example, we can group multiple properties like balance and name into one user account.
Here’s an example of how to define a struct for user accounts:
struct Username {
uint256 balance;
string name;
}
Now, we can create specific users with a balance and a name. For instance:
Username public princechaddha = Username(25000000, "Prince");
This creates a user princechaddha with a balance of 25000000 and the name “Prince”.
25M in the bank less go 🚀 :v
Another Way to Define a Struct
We can also define a struct in a more descriptive way by naming the fields:
Username public princechaddha = Username({balance: 250000000, name: "Prince"});
Both approaches work the same way, but this one is more readable.
Now lets compile the contract, Remix IDE will automatically show a button for each public variable. For example, we will see a button for princechaddha under the contract interface, allowing us to check the balance and name of my bank account 🌚

Adding More Users
Now that we are creating a banking like application there must be more user accounts in the bank. We can add more users in a similar way:
Username public testuser = Username({balance: 100000000, name: "TestUser"});
Now we have two users with balances and names stored in the contract. These users are stored as individual struct instances.
Arrays of Structs
To manage multiple users efficiently, we can create an array of structs. This is useful when we need to handle many accounts.
Username[] public userAccounts;
This array will hold all user accounts, allowing us to manage them collectively.
Dynamic vs Static Arrays
In Solidity, we can create both dynamic and static arrays. The array we’ve been using is dynamic, meaning its size can grow as needed. If we wanted a static array, we can define it like this:
Username[100] public userAccounts;
This would create a fixed-size array that can hold exactly 100 users.
Adding Users to the Array
To add users dynamically, we can write a function that pushes a new user to the userAccounts array:
function createUserAccount(uint256 _balance, string memory _name) public {<br> userAccounts.push(Username(_balance, _name));<br>}
memoryis used for the_nameparameter because strings in Solidity must be stored in memory when they are used inside functions.- The
pushfunction adds a new element (in this case, a newUsernamestruct) to the end of the array.
Let’s put everything together. Here’s the complete code, including the array and struct for a basic bank setup:
pragma solidity 0.8.26;
contract FirstContract {
uint public balance;
uint256[] public accountBalances;
struct Username {
uint256 balance;
string name;
}
//Username public princechaddha = Username({balance: 250000000, name: "Prince"});
//Username public testuser = Username({balance: 100000000, name: "TestUser"});
Username[] public userAccounts; // our aaray
function createUserAccount(uint256 _balance, string memory _name) public {
userAccounts.push(Username(_balance, _name));
} //function to add users
// because there is no user added so far
function deposit(uint amount) public {
balance += amount;
}
function withdraw(uint amount) public {
require(amount <= balance, "Insufficient balance");
balance -= amount;
}
receive() external payable {
balance += msg.value;
}
fallback() external payable {
// Fallback logic
}
}
Now let’s compile the contract, now we will see a button for adding new users. This will ask for input for _balance and _name. Once we provide these inputs, a new user will be added to the userAccounts array.
We’ve now covered arrays and structs in Solidity, and we have learned how they can be used to manage data like user accounts in a simple bank setup. Arrays allow us to store multiple values, while structs let us group related data (like balances and names) together. In the next post, we’ll dive into more advanced concepts like mappings and loops in Solidity.
Stay tuned for the next part!