Open In App

Solidity – Reference Types

Last Updated : 10 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Solidity references store and modify complicated data structures including arrays, structs, maps, and strings. These data types hold a reference to the data’s memory or storage location, unlike value types like integers and booleans. Reference types are vital for developing complicated Ethereum smart contracts since they allow for more data structure and manipulation.

Arrays

Arrays in Solidity are used to store multiple elements of the same type. There are two types of arrays: fixed-size arrays and dynamic-size arrays.

1. Fixed-Size Arrays

Fixed-size arrays have a predetermined length that cannot be changed once declared. They are declared by specifying the type of elements, followed by the array size in square brackets.

Syntax:

<type>[<size>] <array_name>;

Below is the Solidity program to implement the fixed-size arrays:

Solidity




// Solidity program to implement
// the fixed-size arrays
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
 
contract FixedSizeArrayExample {
  uint[5] fixedSizeArray;
   
  function populateArray() public
  {
    for (uint i = 0; i < 5; i++)
    {
      fixedSizeArray[i] = i + 1;
    }
  }
 
  function getArray() public view returns (uint[5] memory)
  {
    return fixedSizeArray;
  }
}


Output: If we call the populateArray() function and then the getArray() function, the output will be [1, 2, 3, 4, 5].

Fixed-Size Arrays

 

2. Dynamic-Size Arrays

Dynamic-size arrays don’t have a predetermined length, allowing their size to change during runtime. They are declared by specifying the element type followed by empty square brackets.

Syntax:

<type>[] <array_name>;

Below is the Solidity program to implement the Dynamic-Size Arrays:

Solidity




// Solidity program to implement
// the Dynamic-Size Arrays
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
 
contract DynamicSizeArrayExample {
  uint[] dynamicSizeArray;
 
  function addElement(uint _value) public
  {
    dynamicSizeArray.push(_value);
  }
   
  function getArray() public view returns (uint[] memory)
  {
    return dynamicSizeArray;
  }
}


Output: Assuming we call addElement(5), addElement(10), and addElement(15) in that order, the output of the getArray() function would be [5, 10, 15].

Dynamic-size Arrays

 

3. Array Members

Array members in Solidity have the following properties:

  • length: It returns the number of elements in the array.
  • push: It adds an element to the end of the array (only for dynamic-size arrays).
  • pop: It removes the elements from the end of a dynamic array of bytes and storage.

Below is the Solidity program to implement the array members’ properties:

Solidity




// Solidity program to implement
// the array members
pragma solidity ^0.5.0;
 
// Creating a contract
contract Types {
     
    // Declaring an array
    uint[] data = [10, 20, 30, 40, 50];
     
    // Defining a function to find the length
    // of the array
    function array_length() public returns(uint)
    {
        uint x = data.length;
        return x;
    }
     
    // Defining the function to push values to the array
    function array_push() public returns(uint[] memory)
    
        data.push(60); 
        data.push(70); 
        data.push(80);
     
        return data; 
    
     
    // Defining a function to pop values from the array
    function array_pop() public returns(uint[] memory)
    
        data.pop();
        return data; 
    }
}


Output:

1. Array Length:

Array Length

 

2. Push:

Push

 

3. Pop:

Pop

 

4. Byte Arrays

Byte arrays are fixed-size or dynamic-size arrays of bytes. They are used to store raw byte data.

Syntax:

bytes<size> <array_name>; // Fixed-size byte array

bytes <array_name>; // Dynamic-size byte array

Below is the Solidity program to implement the byte arrays:

Solidity




// Solidity program to implement
// the byte arrays
pragma solidity ^0.8.0;
 
contract ByteArrayExample {
   
  bytes3 fixedSizeByteArray;
  bytes dynamicSizeByteArray;
 
  constructor()
  {
    // "abc"
    fixedSizeByteArray = 0x616263;
     
    // "defghi"
    dynamicSizeByteArray = hex"646566676869";
  }
 
  function getFixedSizeByteArray() public view returns (bytes3)
  {
    return fixedSizeByteArray;
  }
 
  function getDynamicSizeByteArray() public view returns (bytes memory)
  {
    return dynamicSizeByteArray;
  }
}


Output: After deploying the contract, you can directly call the getFixedSizeByteArray() and getDynamicSizeByteArray() functions, which should return the correct outputs:

  • getFixedSizeByteArray(): 0x616263
  • getDynamicSizeByteArray(): 0x646566676869
Byte Arrays

 

5. String Arrays

String arrays are dynamic-size arrays of strings. They are used to store multiple strings.

Syntax:

string[] <array_name>;

Below is the Solidity program to implement the String arrays:

Solidity




// Solidity program to implement
// the String arrays
pragma solidity ^0.8.0;
 
contract StringArrayExample {
   
  string[] stringArray;
   
  function addString(string memory _value) public
  {
    stringArray.push(_value);
  }
   
  function getArray() public view returns (string[] memory)
  {
    return stringArray;
  }
}


Output:

String Arrays

 

Structs

Structs in Solidity allow you to define custom data structures with various properties. A struct is a composite type that groups different properties (of various types) under a single type. 

Syntax:

struct <structName> {

  <type1> <property1>;

  <type2> <property2>;

  …

}

Example: 

struct Person {

  string name;

  uint age;

}

Below is the Solidity program to implement Structs:

Solidity




// Solidity program to implement Structs
pragma solidity ^0.8.0;
 
contract StructsExample {
  struct Person
  {
    string name;
    uint age;
  }
   
  Person[] public people;
 
  function addPerson(string memory _name,
                     uint _age) public
  {
    Person memory newPerson = Person(_name, _age);
    people.push(newPerson);
  }
 
  function getPerson(uint index) public view returns (string memory,
                                                      uint)
  {
    Person memory person = people[index];
    return (person.name, person.age);
  }
}


This contract defines a Person struct with name and age properties, and an array people to store Person structs. It has two functions: addPerson() which creates a new Person struct and adds it to the array, and getPerson() which returns the name and age of the person at the given index.

Output:

Structs

 

Mappings

Mappings are a key-value data structure in Solidity, similar to associative arrays or hash maps in other programming languages. They allow you to associate a value with a key. 

Syntax:

mapping(<keyType> => <valueType>) <mappingName>;

Example:  

To define a mapping that associates an address with a uint balance, you would use:

mapping(address => uint) public balances;

Below is the Solidity program to implement the Mappings:

Solidity




// Solidity program to implement
// the Mappings
pragma solidity ^0.8.0;
 
contract MappingsExample {
  mapping(address => uint) public balances;
 
  function updateBalance(address _account,
                         uint _newBalance) public
  {
    balances[_account] = _newBalance;
  }
 
  function getBalance(address _account) public view returns (uint)
  {
    return balances[_account];
  }
}


This contract defines a balance mapping that associates an address with a uint balance. There are two functions: updateBalance() which updates the balance of a given account, and getBalance() which returns the balance of a given account.

Output:

Mappings

 

Strings

In Solidity, strings are dynamically-sized arrays of characters, and they are implicitly considered reference types. You can use strings to store and manipulate text data. Solidity does not provide extensive support for string manipulation, so you may need to work with individual characters or convert strings to other types (such as bytes) for more complex operations.

Below is the Solidity program to implement Strings:

Solidity




// Solidity program to implement Strings
pragma solidity ^0.8.0;
 
contract StringsExample {
  string public text;
 
  function setText(string memory _text) public
  {
    text = _text;
  }
 
  function getLength() public view returns (uint)
  {
    return bytes(text).length;
  }
}


This contract defines a text string variable. It has two functions: setText() which sets the value of the text variable, and getLength() which returns the length of the text. Interact with the contract to see the output.

Output:

Strings

 



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads