Does D provide automatic dereferencing for accessing members through pointers?

Andrew Godfrey via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Aug 28 21:37:37 PDT 2014


On Friday, 29 August 2014 at 02:10:46 UTC, H. S. Teoh via 
Digitalmars-d-learn wrote:
> In D you just use '.' throughout and it Just > Works(tm).

Unless the property you're accessing is also a pointer property, 
like
sizeof. Then you have to be careful. The below prints 4 then 8 
(on 32-bit):

unittest {
     import core.stdc.stdlib : malloc, free;

     struct Foo
     {
     	public int bar, baz;
     }

     auto foo = cast(Foo*)malloc(Foo.sizeof);

     import std.stdio;
     writeln(foo.sizeof);
     writeln((*foo).sizeof);

     free(foo);
}

Do pointers have any other cases like this besides 'sizeof'?
I couldn't find a list of pointer properties in the docs (whereas 
I know
where the list of array properties is).


More information about the Digitalmars-d-learn mailing list