6.13 — Void pointers

The void pointer, also known as the generic pointer, is a special type of pointer that can be pointed at objects of any data type! A void pointer is declared like a normal pointer, using the void keyword as the pointer’s type:

void *pVoid; // pVoid is a void pointer

A void pointer can point to objects of any data type:

int nValue;
float fValue;

struct Something
    int nValue;
    float fValue;

Something sValue;

void *pVoid;
pVoid = &nValue; // valid
pVoid = &fValue; // valid
pVoid = &sValue; // valid

However, because the void pointer does not know what type of object it is pointing to, it can not be dereferenced! Rather, the void pointer must first be explicitly cast to another pointer type before it is dereferenced.

int nValue = 5;
void *pVoid = &nValue;

// can not dereference pVoid because it is a void pointer

int *pInt = static_cast<int*>(pVoid); // cast from void* to int*

cout << *pInt << endl; // can dereference pInt

Similarly, it is not possible to do pointer arithmetic on a void pointer. Note that since void pointers can’t be dereferenced, there is no such thing as a void reference.

The next obvious questions is: If a void pointer doesn’t know what it’s pointing to, how do we know what to cast it to? Ultimately, that is up to you to keep track of.

Here’s an example of a void pointer in use:

#include <iostream>

enum Type

void Print(void *pValue, Type eType)
    using namespace std;
    switch (eType)
        case INT:
            cout << *static_cast<int*>(pValue) << endl;
        case FLOAT:
            cout << *static_cast<float*>(pValue) << endl;
        case STRING:
            cout << static_cast<char*>(pValue) << endl;

int main()
    int nValue = 5;
    float fValue = 7.5;
    char *szValue = "Mollie";

    Print(&nValue, INT);
    Print(&fValue, FLOAT);
    Print(szValue, STRING);
    return 0;

This program prints:


In general, it is a good idea to avoid using void pointers unless absolutely necessary, as they effectively allow you to avoid type checking. This allows you to inadvertently do things that make no sense, and the compiler won’t complain about it. For example, the following would be valid:

    Print(&nValue, STRING);

But who knows what the result would actually be!

Although the above function seems like a neat way to make a single function handle multiple data types, C++ actually offers a much better way to do the same thing (via function overloading) that retains type checking to help prevent misuse. Many other places where void pointers would once be used to handle multiple data types are now better done using templates, which also offer strong type checking.

However, very occasionally, you may still find a reasonable use for the void pointer. Just make sure there isn’t a better (safer) way to do the same thing using other language mechanisms first!

7.1 — Function parameters and arguments
6.12 — References vs. pointers, and member selection

21 comments to 6.13 — Void pointers

You must be logged in to post a comment.