0

I got the following code for a contract from a Solidity tutorial website: pragma solidity 0.5.1;

contract MyContractPersonStruct {
    Person[] public people;

    uint256 public peopleCount;

    struct Person {
        string _firstName;
        string _lastName;
    }

    function addPerson(string memory _firstName, string memory _lastName) public {
        people.push(Person(_firstName, _lastName));
        peopleCount += 1;
    }
}

I have got 3 questions:

  1. Person is declared as an array of structure having a variable people. But with ‘people’ we are not using square brackets. Is this not required in Solidity? When can we use square brackets with 'people'?

text box associated with people

  1. I have tried to execute the contract on Remix. I got the above scenario: I can’t understand what values I have give to text box associated with people.

  2. What is the purpose of call in the image above?

Somebody please guide me.

Zulfi.

0

1 Answer 1

1

Person is declared as an array of structure having a variable people. But with ‘people’ we are not using square brackets. Is this not required in Solidity? When can we use square brackets with 'people'?

That's wrong, Person is just a structure type name; Person[] people is an array of structures.

You typically use square brackets in Solidity when you want to access a specific element in the array.

For example, x = people[0] is a read-access to the 1st element in the array, and people[1] = x is a write-access to the second element in the array.

I have tried to execute the contract on Remix. I got the above scenario: I can’t understand what values I have give to text box associated with people.

You need to provide an index, just like you would in Solidity code.

The only difference is that Remix is not Solidity, so you don't have to provide that index inside square brackets.

What is the purpose of call in the image above?

The people array is a public variable in the contract. As such, the Solidity compiler auto-generates a corresponding getter function people(uint256 index) public view returns (string, string) for it, which you can call from the on-chain (i.e., this contract or other contracts) and from the off-chain (e.g., Web3.js script, Web3.py script, Remix, MyEtherWallet, etc) in order to retrieve a specific element in the array.

The call button in the image is what you need to click in order to call this function. Of course, since this function takes uint256 index as input, you need to input an index in the uint256 text-box before calling it.

2
  • Thanks a lot. I am able to understand, 'people' means the entire array and people[0] is the first element, that's why 'people' is used to call push() function. We can't call push() using people[0] because people[0] represents a single element.
    – zak100
    Commented Jan 20, 2020 at 1:31
  • I have a feeling that 'call' is generating a transaction, because in the next example of the tutorial (dappuniversity.com/articles/solidity-tutorial), the author used the require(owner==msg,sender) condition for execution of addPerson() with the help of modifier, but if I change the address of the contract account and press 'call', revert would occur.
    – zak100
    Commented Jan 20, 2020 at 1:36

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.