VOOZH about

URL: https://www.geeksforgeeks.org/solidity/solidity-reference-types/

⇱ Solidity - Reference Types - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Solidity - Reference Types

Last Updated : 28 Apr, 2025

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:


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:


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:


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:


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:


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:


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:


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:


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
 
Comment
Article Tags:

Explore