4

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?

  • 2
    Any of those are fine. I usually do _addr != 0x0 to make it easily identifiable in code. – Adam Kipnis Dec 29 '17 at 17:03
7

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.

  • 2
    In Solidity version 0.5.0 ; comparing with integer 0 is not allowed anymore. – Stef Heyenrath Dec 8 '18 at 11:26
2

In solidity 0.5 this is the correct way to compare addresses

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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