Basics of Addresses and Address Literals

The address type in Solidity is a 20-byte value that represents an Ethereum address. It can hold the address of a user or a contract on the Ethereum network. Here’s an example of declaring and using an address variable in Solidity:

Solidity




// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.0;
  
contract helloGeeks {
    address public userAddress;
  
    function setAddress(address _newAddress) public {
        userAddress = _newAddress;
    }
}


Output

In this example, the userAddress variable of type address is declared as a public variable. The setAddress function takes an address parameter and assigns its value to the userAddress variable.

Additionally, Solidity allows using address literals to specify addresses directly in the code. Here’s an example:

Solidity




// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.0;
  
contract helloGeeks {
    address public userAddress = 0x71C7656EC7ab88b098defB751B7401B5f6d8976F;
}


Output

Address in Solidity

In Solidity, the address type represents Ethereum addresses. Understanding how to work with addresses is essential for developing smart contracts that interact with users and other contracts on the Ethereum blockchain. This article aims to provide a comprehensive guide to addresses in Solidity, accompanied by relevant examples and covering various subtopics.

Similar Reads

Basics of Addresses and Address Literals

The address type in Solidity is a 20-byte value that represents an Ethereum address. It can hold the address of a user or a contract on the Ethereum network. Here’s an example of declaring and using an address variable in Solidity:...

Address vs Address Payable

...

Methods Available with Addresses

...

Type Conversions between Addresses and Address Payable

In Solidity, there are two address types: address and address payable. The address type is used for both user addresses and contract addresses, while the address payable type is specifically used for addresses that can receive Ether. Here’s an example:...

Methods Returning an Address Type

...