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:

 

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:

 

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:

 



Solidity – Reference Types

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.

Similar Reads

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....

Structs

...