Search⌘ K

void* Pointers and Pointers in Logical Expressions

Explore how void pointers in D can reference any data type and the steps to safely convert them to specific types. Understand the use of pointers in logical expressions, including null pointer checks, to control program flow and interaction with C libraries.

void* can point at any type

Although it is almost never needed in D, C’s special pointer type void* is available in D as well. void* can point at any type:

D
import std.stdio;
void main () {
int number = 42;
double otherNumber = 1.25;
void * canPointAtAnything;
canPointAtAnything = &number;
canPointAtAnything = &otherNumber;
writeln(canPointAtAnything);
}

The void* above is able to point at variables of two different types: int and double.

void* pointers are limited in functionality. As a consequence of their flexibility, they cannot provide access to the pointee. ...