0

I am new to solidity. I was trying to compile the following code in chrome browser with Remix. I am getting the following error "Data location can only be specified for array, struct or mapping types, but "memory" was given"

Can anyone tell me what am I doing wrong and how can I fix that problem?

pragma solidity ^0.5.1;

contract ContractExp1{
    string public value;
    uint256 public count=0;

mapping(uint256 => Person) public people;

struct Person{
    string name;
    uint256 balance;
}


constructor() public{
    value='myValue';
}


function setPeople (string memory _name, uint256 memory _val) public {
    count=count+1;
    people[count]=Person(_name,_val);
    }
}
2

Change this:

uint256 memory _val

To this:

uint256 _val

Because uint256 is neither array, nor struct nor mapping.

| improve this answer | |
  • Sorry I just figured it out. Thanks a lot. However, I still would like to know why do we use memory for certain type while not for other type such as int or uint – user3678107 May 21 at 17:59
  • @user3678107: That's how the compiler defines it. If you want to know the reason behind this, then to the best of my understanding, it is because uint256 is passed by value, while arrays, structs and mappings are passed by reference. – goodvibration May 21 at 18:48
  • @user3678107: And BTW, just so you know, your question says Can anyone tell me what am I doing wrong and how can I fix that problem?, and not what you later added in that comment above (about wanting to know why). It is custom here to accept an answer if it has answered your question, rather than changing your question (which BTW, you didn't even do that beyond mentioning it in that comment). – goodvibration May 21 at 18:55
  • Oh. I thought it might be bit redundant if I post a new question so just edited. I agree that my question is answered. However, just was curious to know the memory part since I did not find any tutorial that explained properly the memory part clearly. Instead most of the tutorial I found told that we need to specify memory if we are using Remix. My apology if I did not follow the rules in this site... – user3678107 May 21 at 19:31
  • @user3678107: Well, did you understand my explanation in the comment though (with regards to your question about 'why')? – goodvibration May 21 at 19:39

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.