0

I have a mapping between an address and an array of structs like this...

struct myStruct{
    string name,
    uint height
}

mapping (address=>myStruct[]) myMapping

if I access myMapping with an address key that hasn't been seen before, what gets returned?

e.g.

x = myMapping["unknownKey"];

What is in x? Is it 0 or [] or something else?

2 Answers 2

1

Quoting from solidity docs:

A variable which is declared will have an initial default value whose byte-representation is all zeros. The “default values” of variables are the typical “zero-state” of whatever the type is. For example, the default value for a bool is false. The default value for the uint or int types is 0. For statically-sized arrays and bytes1 to bytes32, each individual element will be initialized to the default value corresponding to its type.

For dynamically-sized arrays, bytes and string, the default value is an empty array or string. For the enum type, the default value is its first member.

https://docs.soliditylang.org/en/v0.8.13/control-structures.html#default-value

So, it should be an empty array.

0

If the value for a specific key provided to a mapping doesn't exist, then it will hold a false value, in this case an empty array.

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.