Anyway to achieve the following
Rekel
paultjeadriaanse at gmail.com
Fri Aug 13 17:11:50 UTC 2021
On Friday, 13 August 2021 at 09:10:18 UTC, Tejas wrote:
> 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?
> 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);
>
>
> }
> ```
That's also what I thought, although at first I thought JG meant
dereferencing a pointer to a type without reallocating the
content.
In a way comparable to aliasing A* or having your original data
be a union in the first place.
It seems however one can use `.` when using pointers, which is
cool, though there seem to be some caveats:
https://forum.dlang.org/post/hthxvxxsxdpkvwcwgisi@forum.dlang.org
(note this is 2014...))
For anyone more experienced with C, I'm not well known with
references but are those semantically similar to the idea of
using a type at a predefined location?
Small sidenote, this would be cool:
```d
int* ip = cast(int*)other_pointer;
int a = #a; // like a dereference but without allocating space
for a elsewhere.
int b = #a; // Or something along those lines
a = 1;
b += 1;
assert(a==2);
```
More information about the Digitalmars-d-learn
mailing list