void *
void * is a pointer to an object of unknown type. It can hold the address of any object and convert back to the original pointer type without loss, which is how C writes code that works on any type.
Detailed Explanation
C has no templates and no generics. When the standard library needs a function that works on any type — allocate memory, sort an array, pass user data to a callback — it uses void *. The type says "this is the address of some object, and I am not telling you what kind".
The conversion rules are what make it usable. In C, any object pointer converts to void * and back implicitly, with no cast and no loss of information, as long as you convert back to a compatible type. This is why the cast on malloc is not merely unnecessary but mildly harmful: it is noise, and in C89 it could hide a missing <stdlib.h> include. C++ is stricter and requires an explicit cast in that direction, which is the single most common source of "this C code does not compile as C++".
Two things void * cannot do:
- It cannot be dereferenced.
*vphas no meaning because the compiler does not know how many bytes to read. You convert to a real pointer type first. - It cannot portably be incremented. Pointer arithmetic scales by the size of the pointed-to type, and
voidhas no size. GCC and Clang allowvp + 1as an extension that treats it as one byte; standard C does not. Cast tounsigned char *when you want to walk bytes.
Where you meet it in practice:
malloc returns void * because it does not know or care what you will store. qsort takes a void * base pointer, an element count, an element size, and a comparator that receives const void * arguments — the size parameter is there precisely because the pointer type no longer carries it. Callback APIs take a function pointer plus a void *user_data that the library passes back untouched, which is how C code carries context into a callback.
The cost is real and worth naming: void * erases type safety. The compiler will let you pass the address of a double to code that will read it as a struct sockaddr, and it will say nothing. Nothing in the type system stops you, and nothing at runtime checks. Every void * in an API is a small contract written in documentation instead of in types, and contracts written in documentation get broken.
So pair it with something. The usual options are a size alongside it (what qsort and memcpy do), a type tag stored in a struct next to the pointer (a discriminated union by hand), or an opaque named type — typedef struct session Session; and pass Session * — which keeps type safety while still hiding the layout. That last option is almost always better than void * when only your own code is involved. Reach for void * when the code genuinely must handle types it has never heard of.
One portability note: void * is guaranteed to round-trip object pointers. Function pointers are a separate category, and converting between a function pointer and void * is not defined by the C standard, even though POSIX requires it to work so that dlsym can exist. Also, char * and void * are required to have the same representation and alignment, a legacy rule from before void * existed.
Code Examples
#include <stdio.h>
#include <stdlib.h>
/* qsort's comparator receives const void *, because qsort has no idea
what it is sorting. You convert back to the real type inside. */
static int compare_ints(const void *a, const void *b)
{
int x = *(const int *)a; /* cast first, then dereference */
int y = *(const int *)b;
return (x > y) - (x < y); /* avoids the overflow that x - y can cause */
}
int main(void)
{
size_t n = 5;
/* malloc returns void *, converted implicitly. No cast needed in C.
The n * sizeof *values idiom cannot get the type wrong. */
int *values = malloc(n * sizeof *values);
if (values == NULL) { /* always check the failure path */
perror("malloc");
return EXIT_FAILURE;
}
values[0] = 30; values[1] = 10; values[2] = 50;
values[3] = 20; values[4] = 40;
/* The element size is a separate argument precisely because the
void * has already thrown the type away. */
qsort(values, n, sizeof *values, compare_ints);
for (size_t i = 0; i < n; i++) {
printf("%d ", values[i]); /* prints: 10 20 30 40 50 */
}
putchar('\n');
free(values);
return 0;
}#include <stdio.h>
/* A callback API. The library knows nothing about the caller's context,
so it carries a void * through untouched. This is the standard C
substitute for a closure. */
typedef void (*RowHandler)(int row_value, void *user_data);
static void for_each_row(const int *rows, size_t count,
RowHandler handler, void *user_data)
{
for (size_t i = 0; i < count; i++) {
handler(rows[i], user_data); /* library does not inspect user_data */
}
}
struct Totals { long sum; int count; };
static void accumulate(int row_value, void *user_data)
{
/* The contract: whoever registered this callback passed a Totals *.
Nothing in the type system enforces it. Keep the cast in one place. */
struct Totals *t = user_data;
t->sum += row_value;
t->count++;
}
int main(void)
{
int rows[] = { 5, 7, 11 };
struct Totals totals = { 0, 0 };
for_each_row(rows, sizeof rows / sizeof rows[0], accumulate, &totals);
printf("%d rows, sum %ld\n", totals.count, totals.sum);
/* prints: 3 rows, sum 23 */
return 0;
}Common Gotchas
`vp + 1` compiles on GCC and Clang but is not standard C
the extension treats `void` as size one. Cast to `unsigned char *` for byte arithmetic if the code must be portable or must build with `-pedantic-errors`.
Do not cast the return value of `malloc` in C
the conversion is implicit, and the cast only adds a place for the type to drift out of sync with the variable. In C++ the cast is required, which is one of the differences between the two languages.