Can not get struct member addresses at compile time

Mike Parker aldacron at gmail.com
Wed Jun 16 11:56:31 UTC 2021


On Wednesday, 16 June 2021 at 09:27:25 UTC, Doeme wrote:

> Is there an alternative to get to this point? Static module 
> initializers are not really an option, since the whole thing 
> should be -betterC.
>

```D
import core.stdc.stdio;
struct Foo{
  ubyte bar;
}

__gshared Foo foo;
void* baz = &foo;
void* bar;

extern(C):

pragma(crt_constructor)
     void initialize() { bar = &foo.bar; }

void main()
{
     *(cast(ubyte*)bar) = 10;
     printf("%d", foo.bar);
}
```

https://dlang.org/spec/pragma.html#crtctor



More information about the Digitalmars-d-learn mailing list