C Notes
Learn about void pointers (generic pointers) in C including declaration, type casting, use cases in generic functions, and limitations with practical examples.
A void pointer (void *) is a special type of pointer that can point to any data type. It's C's version of a generic or universal pointer — you can store the address of an int, float, char, struct, or any other type in a void * variable. However, since the compiler doesn't know what type the pointer refers to, you must explicitly cast it before dereferencing.
Void pointers are the foundation of generic programming in C, used extensively in standard library functions like malloc, qsort, memcpy, and bsearch.
Declaration and Basic Usage
#include <stdio.h>
int main() {
int x = 42;
float f = 3.14;
char c = 'A';
void *vptr; // Can point to anything
// Point to int
vptr = &x;
printf("As int: %d\n", *(int *)vptr); // Must cast before dereferencing
// Point to float
vptr = &f;
printf("As float: %.2f\n", *(float *)vptr);
// Point to char
vptr = &c;
printf("As char: %c\n", *(char *)vptr);
return 0;
}As int: 42 As float: 3.14 As char: A
Why Can't You Dereference a Void Pointer Directly?
The compiler needs to know the data type to:
- Determine how many bytes to read/write
- Interpret the bit pattern correctly
| As (int *): reads 4 bytes | 0x0000002A → 42 |
| As (float *): reads 4 bytes | interprets as float |
| As (char *): reads 1 byte | 0x2A → '*' |
| As (double *): reads 8 bytes | completely different value |
Void Pointers in Memory Allocation
malloc returns void * because it doesn't know what type you'll store:
10 20 30 40 50
Note: In C, casting the return value of malloc is optional (void* implicitly converts to any pointer type). In C++, the cast is required.Writing Generic Functions with Void Pointers
Generic Swap Function
Before: x=10, y=20 After: x=20, y=10 Before: a=1.5, b=2.7 After: a=2.7, b=1.5 Before: s1=Hello, s2=World After: s1=World, s2=Hello
Generic Print Function
42 3.14 Z Hello
Generic Array Search
Found 30 at index 2 Found 3.3 at index 2
Void Pointer Arithmetic
Standard C does not allow arithmetic on void pointers (since the compiler doesn't know the element size). However, GCC allows it as an extension, treating void * like char *:
#include <stdio.h>
int main() {
int arr[] = {10, 20, 30, 40};
void *vptr = arr;
// Standard way: cast to char* for byte-level arithmetic
int *second = (int *)((char *)vptr + sizeof(int));
printf("Second element: %d\n", *second);
// GCC extension (non-portable):
// int *third = (int *)(vptr + 2 * sizeof(int));
return 0;
}Second element: 20
Void Pointer Rules and Limitations
| Feature | Allowed? | Notes |
|---|---|---|
| Assign any pointer to void* | ✅ Yes | Implicit conversion |
| Assign void* to typed pointer | ✅ In C | Implicit in C, explicit cast in C++ |
| Dereference void* | ❌ No | Must cast first |
| Pointer arithmetic | ❌ No | Undefined size; cast to char* first |
| Compare void* pointers | ✅ Yes | ==, != work fine |
| sizeof(void) | ❌ Undefined | GCC gives 1 (extension) |
| void* as function parameter | ✅ Yes | Basis of generic functions |
Practical Use: Implementing a Generic Stack
Popping: 30 20 10
Interview Questions
**Q1: Why does malloc return void* instead of a specific type?**
Because malloc allocates raw memory without knowing how it will be used. Returning void* makes it compatible with any pointer type — the caller decides how to interpret the allocated memory by casting (explicitly or implicitly in C).
Q2: What's the difference between NULL and void pointer?
NULL is a value (typically 0) that represents "points to nothing." A void pointer is a type (void *) that can point to any data type. A void pointer can be NULL or can hold a valid address — they're orthogonal concepts.
Q3: Can you do pointer arithmetic on void pointers in standard C?
No. The C standard does not allow arithmetic on void * because the compiler doesn't know the element size. You must cast to a typed pointer (typically char * for byte-level operations) first. GCC allows it as a non-standard extension.
Q4: How does qsort use void pointers for generic sorting?
qsort takes void *base (the array), size_t nmemb (element count), size_t size (element size), and a comparison function. It manipulates the array using byte-level operations (memcpy with the size parameter) and delegates type-specific comparison to the callback function.
Summary
void *is a generic pointer that can hold the address of any type- You must cast a void pointer before dereferencing it
- Pointer arithmetic is not allowed on void pointers in standard C
malloc,calloc,free,memcpy,qsortall use void pointers for genericity- Pass
size_talongside void pointers so functions know element sizes - Void pointers enable generic data structures and algorithms in C
- In C (unlike C++), void pointers implicitly convert to/from any pointer type
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Void Pointers in C - Generic Pointers Guide.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this C Programming topic.
Search Terms
c-programming, c programming, programming, pointers, void, void pointers in c - generic pointers guide
Related C Programming Topics