www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How to get the address of a static struct?

reply FoxyBrown <Foxy Brown.IPT> writes:
static struct S
{
    public static:
         int x;
         string a;
}


....


auto s = &S; // ?!?!?! invalid because S is a struct, but...


basically s = S. So S.x = s.x and s.a = S.a;

Why do I have to do this?

Because in visual D, global structs don't show up in the 
debugger. So if I create a local alias, I can see see them, but I 
don't wanna have to do one variable for each variable in the 
struct...
Jul 09 2017
next sibling parent rikki cattermole <rikki cattermole.co.nz> writes:
Thread local struct:

```D
module foobar;

struct Foo {
	int x;
}

Foo foo = Foo(8);

void main() {
	import std.stdio;
	writeln(&foo);
}
```

Global struct:

```D
module foobar;

struct Foo {
	int x;
}

__gshared Foo foo = Foo(8);

void main() {
	import std.stdio;
	writeln(&foo);
}
```

Compile time constant:

```D
module foobar;

struct Foo {
	int x;
}

enum FOO = Foo(8);

void main() {
	import std.stdio;
	Foo foo = FOO;
	writeln(&foo);
}
```
Jul 09 2017
prev sibling parent Era Scarecrow <rtcvb32 yahoo.com> writes:
On Monday, 10 July 2017 at 03:48:17 UTC, FoxyBrown wrote:
 static struct S

 auto s = &S; // ?!?!?! invalid because S is a struct, but...

 basically s = S. So S.x = s.x and s.a = S.a;

 Why do I have to do this?
Static has a different meaning for struct. More or less it means it won't have access to a delegate/fat pointer to the function that uses it. It doesn't mean there's only 1 instantiation ever (unlike like the static variables). So static is a no-op in this case (though still syntactically legal to use). To get the address of the struct you STILL have to instantiate it first. Although you don't to in order to access it's static members. Though if all the members are static, it's basically a namespace and doing so is kinda pointless.
Jul 09 2017