How to verify if a void pointer (void *) is one of two data types?

The translation of void* is
“Dear compiler, this is a pointer, an there is no additional information for you on this.”.

Usually the compiler knows better than you (the programmer), because of information he got earlier and still remembers and you might have forgotten about.
But in this special case, you know better or need to know better. In all cases of void* the information is available otherwise, but only to the programmer, who “happens to know”. The programmer therefor has to provide the information to the compiler – or better to the running program, because the one advantage a void* has is that the information can change during runtime.
Usually that is done by giving the information via additional parameters to functions, sometimes via context, i.e. the program “happens to know” (e.g. for each possible type there is a separate function, whichever function gets called implies the type).

So in the end void* does not contain the type info.
Many programmers misunderstand this as “I don’t need to know the type info”.
But the opposite is true, the use of void* increases the responsibility of the programmer to keep track of the type info and provide it appropriatly to the program/compiler.

Leave a Comment