For example, for:

mapping(address => bytes32) public addressName;

which one of the following syntax is correct?

address _address = 0x...; //an actual address

In Solidity mappings, all possible keys are assigned 0 by default.

So if a valid value in your use case is always non-zero, then you can check with:

addressName[addr] != 0

And if 0 is a valid value, then by definition it's already been initialized to that at the beginning.

  • When I check a new key, the value of addressName[new_key] is "0x0000000000000000000000000000000000000000000000000000000000000000". Does it have the same value of 0?
    – Daniel
    Nov 9 '18 at 4:37
  • @Daniel, yes, it's the same.
    – Bastien
    Nov 9 '18 at 7:57

addressName[address]!=0 will work to check if the address has been assigned a value. this assumes that 0 is not a valid/possible assignment in your application.

 function test(address addr, bytes32 email) public {
        require(addressName[addr] == 0); // if 0 then no assignment has taken place, if 1 the address has been registered (reverse)
        addressName[address] = email;  // map the address to the email

Hope this helps


address is a reserved keyword (a type), so addressName[address] is not even going to compile.

If we replace it with name, for the sake of this answer, then you can try something like this:

bytes(addressName[name]).length > 0
  • @Briomkez: Well, I admit that I haven't even tried to implement and compile a function which refers to addressName[address], so I might be wrong. But can you please provide an example of such function? It should be something like function func(address address) external view returns (bool), which if to be honest, I still can't see how it would compile (and it would be a very confusing prototype even if it does compile somehow). Nov 8 '18 at 9:48
  • I cancelled the comment because I read the answer wrongly. Sorry
    – Briomkez
    Nov 8 '18 at 12:08
  • @goodvibration Reserved keywords are, in pretty much every language, only relevant when that's the full name. If the variable was named address, uint, or even function, those wouldn't work, but addressName is valid.
    – natewelch_
    Nov 8 '18 at 22:05
  • Sorry for the misunderstanding, the "address" in "addressName[address]" represents an actual address.
    – Daniel
    Nov 9 '18 at 4:25
  • @goodvibration When I try bytes32(addressName[name]).length > 0 where name is a new address which hasn't been assigned value, it returns true.
    – Daniel
    Nov 9 '18 at 4:34

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.