I tried to declare string inside my function but it doesn't work.
How to declare string inside function?
Ethereum Stack Exchange is a question and answer site for users of Ethereum, the decentralized application platform and smart contract enabled blockchain. It only takes a minute to sign up.
Sign up to join this communityI suspect it has to do with some already answered question like Why do Solidity examples use bytes32 type instead of string?
However, a code like the one below seems to work around without declaring a string.
contract A{
function f() constant returns(string){
var s="hello";
return s;
}
}
looms like compiler is not getting the righ context for the string declaration.
As such below default rules applies to decide on the storage option based on context: - default storage for member variable is 'storage' - default for function parameters and return types is 'memory'
In EVM context, string is an array which has an extra annotation -'data location'. Thsi could be either 'memory' or 'storage'.
Seems in the below programm, it exolicitly looking to define the storage option.
As you can see below, I specified the stogare option as 'memory' and its compiling fine.
pragma solidity ^0.4.4;
contract Test {
function print() constant returns(string){
string memory x = "ddd";
return x;
}
}