Void Pointers
Challenge Gallery
Quick Reference
void * basics:
int x = 42;
void *vp = &x; // Any pointer converts to void *
int *ip = (int *)vp; // Must cast back to use
int val = *(int *)vp; // Cast and dereference
Common standard library uses:
| Function | void * Usage |
|---|---|
qsort |
Array pointer + comparator params |
memcpy |
Source and destination pointers |
memset |
Buffer pointer |
calloc/malloc |
Return type |
bsearch |
Array + key pointers |
Common Pitfalls
- Dereferencing without cast –
*vpwon’t compile. Always cast:*(int *)vp. - Wrong cast – Casting to the wrong type reads garbage. If data is int, cast to
int *, notdouble *. - Pointer arithmetic – Not allowed on void * in standard C. Cast to
char *for byte-level arithmetic. - Type safety gone – void * bypasses type checking. The compiler can’t catch type mismatches — you must be careful.