What is a void pointer in C?

A void * is a generic pointer. It can store the address of any type, but you cannot dereference it directly. You must cast it to the right type first.

#include <stdio.h>

int main(void) {
    int n = 42;
    float f = 3.5f;
    void *p;

    p = &n;
    printf("%d\n", *(int *)p);
    p = &f;
    printf("%.1f\n", *(float *)p);
    return 0;
}

Output:

Output of the above program is

42
3.5

malloc() returns a void *, and functions like qsort() and memcpy() use it to work with any type.


Learn more about the similar topics:
Tutorials
No Content Found.
Exercises & Assignments
No Content Found.
Interview Questions & Answers
No Content Found.