Void Pointer in C

A void pointer in C is a generic pointer type that can store the address of any data type. It is declared using void * and is often called a generic pointer because it is not tied to a specific type such as int *, char *, or float *.

Void pointers are useful when a program needs to work with memory in a flexible or type-independent way. But that flexibility also comes with rules. A void pointer cannot be dereferenced directly without casting it to the correct type first. In this article, we will understand void pointer in C, its syntax, how it works, why it is used, how casting is involved, and the common mistakes programmers make with it.

What is Void Pointer in C?

A void pointer in C is a pointer that can hold the address of any type of variable. Because it is not associated with a specific data type, it acts as a general-purpose address holder.

This is why a void pointer is commonly called a generic pointer.

A void pointer can point to any data type, but it must be cast to the correct type before dereferencing.

Syntax of Void Pointer in C

The syntax is straightforward:

void *pointer_name;

Example:

void *ptr;

This declaration means ptr can store the address of different kinds of variables.

How Void Pointer Works in C

A void pointer stores an address, but it does not know what kind of data exists at that address. Since the compiler does not know the pointed data type, it cannot decide how many bytes should be read when dereferencing.

  1. The void pointer stores an address.
  2. The program decides what actual type exists at that address.
  3. The pointer is cast to the correct type.
  4. After casting, the value can be accessed safely.

This is the most important rule in the whole topic: a void pointer is flexible, but it does not remove the need to know the actual data type.

Example of Void Pointer in C

The following example shows how one void pointer can store addresses of different variable types.

#include <stdio.h>

int main(void)
{
    int x = 10;
    float y = 5.5f;
    char ch = 'A';
    void *ptr;

    ptr = &x;
    printf("%d\n", *(int *)ptr);

    ptr = &y;
    printf("%.1f\n", *(float *)ptr);

    ptr = &ch;
    printf("%c\n", *(char *)ptr);

    return 0;
}

Here, the same void pointer is used to hold addresses of an integer, a float, and a character. Each time the pointer is cast to the correct type before dereferencing.

Why Void Pointer Cannot Be Dereferenced Directly

The compiler must know the size and type of the data being accessed. A void pointer does not carry that information. So direct dereferencing like this is invalid:

void *ptr;
/* *ptr;  invalid */

Before dereferencing, you must cast the pointer:

printf("%d\n", *(int *)ptr);

Void Pointer and Type Casting in C

Type casting is central to void pointers. Since the compiler cannot infer the real pointed type from void *, the programmer must tell it what the type is.

Stored actual typeCorrect cast
int(int *)ptr
float(float *)ptr
char(char *)ptr
double(double *)ptr

If the wrong cast is used, the program may interpret memory incorrectly and produce undefined behavior.

Where Void Pointer is Used in C

  • generic function arguments
  • dynamic memory allocation functions such as malloc()
  • low-level memory utilities
  • data structures that must store multiple kinds of data
  • library code that should work with different data types

One very common real-world use is malloc(), which returns a void * because it gives memory without knowing what data type the programmer will store there.

Void Pointer with malloc() in C

The memory returned by malloc() is a classic example of void pointer use.

#include <stdlib.h>

int *ptr = (int *)malloc(sizeof(int));

malloc() returns a void *, and the program treats that memory as integer storage by using an integer pointer.

Can Arithmetic Be Done on Void Pointer in C?

In standard C, pointer arithmetic on void * is not defined the same way as typed pointers because void has no size. Since the compiler does not know the element size, moving a void pointer directly is not standard behavior.

If memory movement is needed, the pointer is usually cast to char * or another appropriate type first.

char *cptr = (char *)ptr;
cptr++;

This works because char has a size of one byte, so pointer movement becomes well-defined.

Advantages of Void Pointer in C

  • supports generic programming patterns
  • works with addresses of multiple data types
  • useful in library and utility functions
  • important in memory allocation and low-level code
  • improves flexibility in data handling

Common Mistakes with Void Pointer in C

  • trying to dereference a void pointer directly
  • using the wrong cast before dereferencing
  • assuming a void pointer knows the data type automatically
  • performing invalid pointer arithmetic on void *
  • forgetting what actual type was originally stored
MistakeProblemSafer idea
Direct dereference of void *Compiler cannot know the data typeCast to the correct type first
Wrong castReads memory incorrectlyTrack the real data type carefully
Arithmetic on void *Not standard and often unsafeCast to char * or another valid type first

Best Practices for Void Pointer in C

  • Always know the real data type behind the void pointer.
  • Cast to the correct type before dereferencing.
  • Do not use void pointers when a typed pointer gives clearer code.
  • Use void pointers where generic behavior is actually needed.
  • Be extra careful in memory and library code.

FAQs

What is void pointer in C?

A void pointer in C is a generic pointer that can store the address of any data type.

Why is void pointer called a generic pointer in C?

Because it is not tied to a specific data type and can hold addresses of different kinds of variables.

Can we dereference a void pointer directly in C?

No. A void pointer must be cast to the correct type before dereferencing.

Can a void pointer point to any type in C?

Yes. A void pointer can hold the address of different data types such as int, float, char, or other objects.

Why is casting needed with void pointer in C?

Casting is needed because the compiler must know the actual data type before reading or interpreting the memory correctly.

Can pointer arithmetic be done on void pointer in C?

In standard C, pointer arithmetic on void * is not properly defined like typed pointers. It is safer to cast it to char * or another correct type first.