Web3 Afrika

Web3 Afrika

Array struct in solidity

Array struct in solidity

Michael Fawole's photo
Michael Fawole
·Oct 6, 2022·

2 min read

Table of contents

  • What is an array?
  • Types of array in solidity
  • What is Struct
  • Array-struct

In this article, I will be explaining explicitly what we call array struct in solidity and how they work in solidity coding.

Before we talk about array-struct, you should know what array is and what struct is.

What is an array?

An array is a data structure which saves collections of elements of the same data type.

Types of array in solidity

  1. Fixed array
  2. Dynamic array

Let take it one by one.

  1. Fixed array is an array that has a predefined size. For example
    uint[3] public learn = [1,2,3];
    

uint is the data type while the square bracket that has 3 inside is the fixed array telling us that the size of the array is 3, "public" is the visibility qualifier while learn is the name of the variable holding the array.

2 Dynamic array is an array whose size is unlimited. For example

 uint[] public learn = [1,2,3];

In fixed array we specified the array size but in dynamic array, you leave the bracket empty.

What is Struct

Structs in Solidity enables us to create more complicated data types that have multiple properties.

To write struct in solidity you start with struct, then you mention the name of the struct, followed by curly bracket.

For example

struct web3Africa{

}

Next is to fill the struct with details like name, age and height.

struct web3Africa{
string name;
uint age;
uint height;
}

name variable type is string while that of age and height is uint.

Numbers are given uint while alphabet or alphanumric are string.

Array-struct

Array struct is the combination of struct and array.

The formula for array struct is given below

struct-name[] public name-of-array;

Let now apply the struct name and give the array any name we want.

web3Africa[] public intel;

Let see the full code

pragma solidity ^0.5.0;

contract Michael{


    struct web3Africa{
        string name;
        uint age;
    }

web3Africa[] public intel;


}

Congratulations, you have learnt array struct in solidity.

 
Share this