0

In the function registerUser below, I am trying to access a global array of structs, and seeing if the argument I have passed exists in the global array or not. The code is as follows:

user_details[] user_list;    //The global array

function registerUser(user_details memory _user) public {

    // check if user does not exist
    bool found = false;

    for (uint i = 0; i < user_list.length; i++) {
        if (user_list[i] == _user)
            found = true;
    }
}

However, it is giving me the following error: Operator == not compatible with types struct LandInit.user_details storage ref and struct LandInit.user_details storage pointer

Is there a possibility of casting ref as pointer? Or any other fix? I tried creating new struct in the function and copying the function argument to it, but it still gives an error.

1 Answer 1

0

There are multiple ways of doing this.

As of now, there is no intrinsic way to check if struct user_details a == user_details b.

  1. You may want to write a function that checks for a single or multiple elements of a struct. Consider either using all the variables or using a uniqueId.
function equals(user_details memory _userA, user_details memory _userB) internal view returns (bool) {
        // Just compare the output of the hashed field
        return(keccak256(abi.encodePacked(_userA._attributeA, _userA._attributeB)) == keccak256(abi.encodePacked(_userB._attributeA, _userB._attributeB)));
    }
  1. You can make a mapping to structs and include a boolean in the struct. Or more uniquely, a mapping to hashes. I would invite you to check Rob's answer below. He explains it very well.

Sources:

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.