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{

function setPeople (string memory _name, uint256 memory _val) public {

2 Answers 2


Change this:

uint256 memory _val

To this:

uint256 _val

Because uint256 is neither array, nor struct nor mapping nor string.

  • 2
    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 Commented May 21, 2020 at 17:59
  • 3
    @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. Commented May 21, 2020 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). Commented May 21, 2020 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... Commented May 21, 2020 at 19:31
  • @user3678107: Well, did you understand my explanation in the comment though (with regards to your question about 'why')? Commented May 21, 2020 at 19:39

Fix: Remove memory from uint256 memory _val .

Reason: As described in the error , memory/calldata data locations can only be assigned to array,struct or mapping data types. String is treated as an array of bytes, because like an array of numbers, string is an array of byte sized characters.And the length of a string is arbitrary just like that of an array. But uint256 is of a fixed size (256 bits)

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.