On Friday, 13 August 2021 at 08:25:33 UTC, JG wrote:
Suppose one has a pointer p of type T*.
Can on declare variable a of type T which is stored in the location pointed to by p?

As an example if we have:

    struct S
    {
      int x = 1234;
    }

    void main() {
       S s;
       //unknown construction of a using &(s.x)
       writeln(a); //displays 1234
       s.x = s.x+1;
       writeln(a); //displays 1235
       a = a +1;
       writeln(s.x); //displays 1236
    }
----------------------------------------------------------------
Similar behavior can be achieved in the body of the lambda here

    import std.stdio;

    struct S
    {
      int x = 1234;
    }


    void main() {
        S s;
        (ref a){
             writeln(a);
             s.x = s.x + 1;
             writeln(a);
             a = a +1;
             writeln(s.x);
        }(s.x);
    }

Umm is this what you want?
```d
import std.stdio;

struct S
{
  int x = 1234;
}


void main() {
    S s;
    /*(ref a){
         writeln(a);
         s.x = s.x + 1;
         writeln(a);
         a = a +1;
         writeln(s.x);
    }(s.x);*/

    auto a = &(s.x);
    writeln(*a);
    s.x += 1;
    writeln(*a);
    *a += 1;
    writeln(s.x);


}
```

Reply via email to