11

I remember reading that the correct way to check if an address is 0x0 or not is _addr == address(0) and not _addr == 0. However, I'm unable to find any difference between the two when I run the following:

pragma solidity ^0.4.19;

contract Test {
    bool public isResult1 = false;
    bool public isResult2 = false;

    function isAddressZero1(address _addr) public {
        isResult1 = (_addr == address(0));
    }

    function isAddressZero2(address _addr) public {
        isResult2 = (_addr == 0);
    }
}

Is there a known issue with _addr == 0 or is the explicit typecasting just to be safe?

1
  • 3
    Any of those are fine. I usually do _addr != 0x0 to make it easily identifiable in code. Commented Dec 29, 2017 at 17:03

2 Answers 2

13

These should be completely equivalent. I think some people may prefer 0x0 or address(0) or address(0x0) as some sort of convention (to make it a little clearer what kind of 0 you mean), but they all should compile to the same thing.

Solidity Version 0.5.0 onwards, you cannot compare directly with 0 or 0x0 or "0" or "0x0". Since these types are not treated as address.

1
  • 4
    In Solidity version 0.5.0 ; comparing with integer 0 is not allowed anymore. Commented Dec 8, 2018 at 11:26
6

In solidity 0.5 this is the correct way to compare addresses

require(address(_registry) != address(0));   
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.