Derived data types
In this section, we're going to observe D's take on pointers, arrays, strings, and associative arrays. Much of what we'll cover here is very different from other C-family languages.
Pointers
As in other languages that support them, pointers in D are special variables intended to hold memory addresses. Take a moment to compile and run the following:
int* p; writeln("p's value is ", p); writeln("p's type is ", typeid(p)); writeln("p's size is ", p.sizeof);
First, look at the declaration. It should look very familiar to many C-family programmers. All pointer declarations are default initialized to null
, so here the first call to writeln
prints "null"
as the value. The type of p
printed in the second writeln
is int*
. The last line will print 4
in 32-bit and 8
in 64-bit.
So far so good. Now look at the following line and guess what type b
is:
int* a, b;
No, b
is not an int
, it is an int*
. The equivalent C or C++ code would look like this:
int *x, *y;
In D, x
would be interpreted as...