You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The following code throws an error with message Data location must be "memory" or "calldata" for parameter in external function, but "storage" was given.
function f(S storages) externalview {
}
The after changing external into public, I got Data location must be "memory" or "calldata" for parameter in function, but "storage" was given.. It's a simple typo I think. We need to specify that no storage in the parameter list of a public function.
Environment
Compiler version: 0.8.28
The text was updated successfully, but these errors were encountered:
You're right; Solidity does not allow parameters to be passed as storage in external or public functions. This is because storage variables are meant to refer to storage in the contract itself, so they can't be used directly as function parameters. Instead, parameters should be passed as memory or calldata in functions that are external or public.
If you want to work with a storage variable within the function, you should either pass a memory or calldata copy of it or declare the storage variable outside the function. Here’s an example of how you can modify your code:
solidity
Copy code
struct S {
uint256 value;
}
S s; // Declared in storage, so we can refer to it
function f() external view returns (uint256) {
// Directly use the storage variable s here without passing it as a parameter
return s.value;
}
Alternatively, if you need to pass a struct as a parameter, you can use memory as the data location:
solidity
Copy code
function f(S memory s) public pure returns (uint256) {
return s.value;
}
This way, the struct s is passed by value (copying the data to memory), and the function can work with it.
@Shivu7889 Thanks for your comments. But I actually mean that if we pass external parameters in a public function, the error message is misleading. The compilers said, Data location must be "memory" or "calldata" for the parameter in the function, but "storage" was given.. It should specify that this regulation happens under public and external functions. I think it's an error handling typo
Description
The following code throws an error with message
Data location must be "memory" or "calldata" for parameter in external function, but "storage" was given.
The after changing
external
intopublic
, I gotData location must be "memory" or "calldata" for parameter in function, but "storage" was given.
. It's a simple typo I think. We need to specify that nostorage
in the parameter list of apublic
function.Environment
The text was updated successfully, but these errors were encountered: