A void pointer in C, written void *, is a generic pointer that can hold the address of an object of any data type. Because it has no type, you cannot dereference it or do pointer arithmetic on it directly: you must first convert it to a concrete pointer type such as int *. A void pointer in C is what makes functions like malloc, memcpy and qsort work with any type.
This guide shows how to declare and convert void pointers, how to build a generic function with them, and which rules and pitfalls to remember.
A void pointer is declared like any other pointer, with void as the pointed-to type:
The same variable p points to three different types in turn. To read the value you cast to the right type before dereferencing, as in *(int *)p. The compiler cannot check that the cast is correct, so you are responsible for remembering what p actually points to.
| Operation | Allowed? | Notes |
|---|---|---|
Assign any object pointer to void * | Yes | No cast needed |
Assign void * to another object pointer | Yes in C | No cast needed in C, required in C++ |
Dereference *p | No | The type has no size or layout |
Pointer arithmetic p + 1 | Not in standard C | GCC allows it as an extension, treating the size as 1 |
Compare with NULL or other pointers | Yes | Works as usual |
Convert a function pointer to void * | Not portable | The standard does not guarantee it |
Two details are worth remembering. First, in C you do not need to cast the result of malloc, and many style guides advise against doing so, because the cast can hide a missing #include <stdlib.h> in old code. Second, to move through memory byte by byte, convert to unsigned char * or char * and do the arithmetic there.
The functions you already use rely on void *:
Here is qsort sorting integers, where the comparison function receives const void * and casts back to the real type:
The expression (x > y) - (x < y) returns -1, 0 or 1 and avoids the overflow that x - y could cause.
Because a void * carries no size, a generic function normally takes the element size as an extra argument and works in bytes. This function reverses any array in place:
The pattern is: accept void * and a size, convert once to unsigned char *, then compute each element's address as base + index * size.
A void pointer trades compile-time checking for flexibility, so the usual bugs are your responsibility.
double through an int * is undefined behaviour and returns garbage. Keep track of the real type.void * that came from a char buffer may not be suitably aligned for int or double. Memory from malloc is aligned for any type, but a plain char array is not guaranteed to be.*p on a void * is a compile error, and p++ is non-standard.const: converting const void * to a non-const pointer and writing through it is undefined behaviour if the original object was const.void * does not know how large its object is. Always pass the length or size alongside it.If you only need a handful of types, a tagged struct with a union, or C11 _Generic, gives compile-time checks that void * cannot.
The compiler does not know the size or type of the object it points to, so it cannot decide how many bytes to read or how to interpret them. Cast to a typed pointer first.
Not in standard C. GCC and Clang accept it as an extension and treat the element size as one byte, but portable code should use unsigned char * instead.
No. A void * converts to any object pointer type implicitly in C. In C++ the cast is required, but C++ code normally uses new or containers.
void means no value or no parameters. void * is a real pointer type that holds an address of unspecified type.
A void pointer in C is a typeless, generic pointer that can hold the address of any object. You must convert it to a concrete pointer type before dereferencing, and you should do byte-wise arithmetic through unsigned char *. It powers malloc, memcpy and qsort, and lets you write generic functions by passing the element size. In exchange you lose type checking, so track the real type, respect alignment and const, and always pass sizes explicitly.