Looks like dereferencing a null pointer, but is ok???

Steve Horne stephenwantshornenospam100 at aol.com
Wed Sep 20 22:27:59 PDT 2006


On Tue, 12 Sep 2006 00:03:55 +0300, Georg Wrede
<georg.wrede at nospam.org> wrote:

>And if really not, what in the world is
>
>    (cast(char*)null)[0..10]
>
>supposed to mean, or return???

Reminds me of the definition of the offsetof macro in C.

Cast a null pointer to the type of the struct, then take the address
of the needed field, then cast to integer.

Sure, you're dealing with silly addresses - so long as you don't read
or write to them, there's no error. And all you're doing is getting an
offset, presumably to use in pointer arithmetic later on.

How you'd get a practical advantage from your example, I don't know,
but so long as the language doesn't outlaw things on principle, you
can often find useful tricks like that.

-- 
Remove 'wants' and 'nospam' from e-mail.



More information about the Digitalmars-d-learn mailing list