void Pointers & Generic Code in C
void* is a pointer to unknown type — it holds any object address but forbids dereferencing and arithmetic until cast back to a concrete type. This is C's generics mechanism: qsort, callbacks with context, and container libraries all traffic in void* to stay type-agnostic.
The contract is bookkeeping by the programmer: whoever casts back must cast to the right type (the compiler can't verify), and arithmetic needs a concrete cast first (char* for byte stepping, the GNU extension aside). void* carries addresses, never type information.
Holds anything, knows nothing
void *p = &x; compiles for any object pointer — but *p and p++ are illegal until p becomes a concrete pointer type again.
Cast back to use
*(int *)p reads the int — the cast documents the programmer's knowledge the compiler cannot check.
Generic interfaces run on it
qsort comparators receive const void*; callback context travels as void* — one signature serving every type.
No arithmetic without casting
Step bytes via (char *)p + n after casting — void has no size, so the compiler can't scale the step.
#include <stdio.h> void print_int(void *p) { printf("%d\n", *(int *)p);} int main(void) { int x = 42; print_int(&x); return 0;}Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Re-implement each visualized algorithm in C yourself — building beats watching.