2

In a contract, using Solidity, is it possible to store a pointer to a storage variable in a storage variable (or somewhere else that may be suitable) and access it from distinct functions?

Consider the following example in Solidity pseudo code:

contract AContract {

    struct AStruct { 
      uint a_field;
    }

    AStruct variable_a;
    AStruct variable_b;

    AStruct storage pointer_to_either_a_or_b;

    modifier AModifier()
    {
      if (condition_a)
      {
        pointer_to_either_a_or_b = variable_a;
      }
      else
      {
        pointer_to_either_a_or_b = variable_b;
      }
    }

    function AFunctionUsingPointerToAorB100() internal AModifier()
    {
      pointer_to_either_a_or_b.a_field = 100;          
    }

    function AFunctionUsingPointerToAorB200() internal AModifier()
    {
      pointer_to_either_a_or_b.a_field = 200;          
    }

}

Whenever I try to use the "storage" keyword for a storage variable I get the following compilation error for "AStruct storage pointer_to_either_a_or_b":

Error: Expected identifier, got 'Storage'

Is there a way to circumvent this, like placing the pointer somewhere else in the code?

  • 1
    State variables are ALWAYS storage variables so you can't add that to AStruct storage pointer_to_either_a_or_b; declaration. It's already a storage variable. – pabloruiz55 Oct 4 '17 at 17:53
1

Removing the keyword storage at AStruct storage pointer_to_either_a_or_b; would solve the error (as state variables are always storage ), but you won't be able to achieve what you are trying to do.

The reason is; since AStruct pointer_to_either_a_or_b; is a storage variable, assigning a value to it will create an independent copy, not an reference to variable_a or variable_b.It's mentioned in the solidity docs here,

assignments between storage and memory and also to a state variable (even from other state variables) always create an independent copy.

I don't know what your actual requirement is, but depending on what you have posted here, I won't use a modifier(and no pointers) but would try something like this,

function sample(uint _val) public // val = 100 or 200
 {
      if (condition_a)
      {
        variable_a.a_field = _val;
      }
      else
      {
        variable_b.a_field = _val;
      }
}

Hope this helps!

FYI: As I know local variables declared inside modifiers are not accessible inside the function.

  • Thank you for your reply. I suspected it wouldn't be possible to access a shared a pointer to a storage variable within distinct functions. The actual requirement is having a set of many functions selecting a distinct variable as output, depending on a given condition. I thought of modifiers as a hypothetically good option for creating a pointer to those output variables, but as you stated it's not possible to access variables declared inside modifiers inside functions. – Paulo Oct 6 '17 at 10:12
  • I will likely have to use something similar to your suggestion, like this, using an additional function to return a pointer: function Greeter_a(uint _val) public // val = 100 or 200 { GetPointer().a_field = _val; } (...) function Greeter_x(uint _val) public // val = 100 or 200 { GetPointer().x_field = _val; } function GetPointer() public returns (VariableType storage) { if (condition_a) { return variable_a; } else { return variable_b; } } – Paulo Oct 6 '17 at 10:12
  • I'm afraid, you can do this with getPointer() because the returned value will be a copy of the variable and it won't be pointing to the state variable and modify it – Achala Dissanayake Oct 7 '17 at 6:37
  • When adding the "storage" keyword to "returns", it actually does return a reference to the storage variable, like a pointer. So, "returns (VariableType storage)" appears to work. – Paulo Oct 9 '17 at 9:26

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.