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?

rohithpr
  • 418
  • 2
  • 4
  • 16

2 Answers2

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.

Ashutosh Singh
  • 351
  • 1
  • 8
user19510
  • 27,999
  • 2
  • 30
  • 48
6

In solidity 0.5 this is the correct way to compare addresses

require(address(_registry) != address(0));   
Rosco Kalis
  • 2,137
  • 2
  • 14
  • 26
Shahzad Aslam
  • 61
  • 1
  • 1