Anyway to achieve the following
Tejas
notrealemail at gmail.com
Fri Aug 13 09:10:18 UTC 2021
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);
}
```
More information about the Digitalmars-d-learn
mailing list