Get familiarized with the pointer syntax in D.

The pointer syntax of D is almost the same as in C. Although this can be seen as an advantage, the peculiarities of C’s pointer syntax are also inherited by D For example, the different meanings of the * character may be confusing.

With the exception of void pointers, every pointer is associated with a certain type and can point at only variables of that specific type. For example, an int pointer can only point at variables of type int. The pointer definition syntax consists of the associated type and a * character:

type_to_point_at * name_of_the_pointer_variable;

Accordingly, a pointer variable that points at int variables is defined like this:

int * myPointer;

The * character in this syntax may be pronounced as “pointer.” So, the type of myPointer above is an “int pointer.” The spaces before and after the * character are optional. The following syntaxes are common as well:

int* myPointer;
int *myPointer;

When it is specifically a pointer type that is being mentioned as in “int pointer”, it is common to write the type without any spaces as in int*.

Get hands-on with 1000+ tech skills courses.