Empty of Struct, String, Array or Mapping in Solidity

Hamilton Cyber
2 min readOct 20, 2022

--

There is no concept of NULL in solidity. Empty of a struct, string, array or mapping in solidity is treated as zero-value of each type, e.g. zero of unit type is 0.

string

Normally, an empty string means its length is 0. But unfortunately, string in solidity has no attribute or function to retrieve its length. Two following ways can be used to accomplish this:

string x;
if(bytes(x).length == 0) {……}
// is empty


or

string x;
if (sha3(x) != sha3(“”)) {……}
// is not empty

struct

In solidity, a struct does not need to be new’ed for its members to be accessible, where all the members are empty with their members having zero-value of each type.

struct Transaction {
address to;
unit256 value;
bytes data;
}
Transaction private tx;


Members of tx can be accessed even tx has been assigned, tx.to has its zero-value: 0.

array

An empty array is an array with length of 0.

address[] memory addrList;
if (addrList.length == 0) {……}
// is empty

mapping

Array and mapping are accessible immediately after declaration.

mapping(string =>Transaction ) txMapping;

if (txMapping[“xx”].value == 0) {……}


Although the Transaction struct object txMapping[“xx”] has not been assigned, the ‘value’ member of this ‘virtual’ object can be accessed directly.

Source:

hamilton.cyber’s github : /EtherreumDevArticles/blob/main/Notes/Empty_of_Struct_String_Array_Mapping.md

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Hamilton Cyber
Hamilton Cyber

Written by Hamilton Cyber

0 Followers

Participants of blockchain technology, developer, thinker and strategier.

No responses yet

Write a response