๐Ÿ“ C Pointers
Estimated reading: 3 minutes 267 views

๐Ÿ”ณ C void Pointer โ€“ The Generic Pointer in C


Introduction โ€“ What Is a void Pointer in C?

In C programming, a void pointer (also called a generic pointer) is a special type of pointer that can point to any data type. It is declared as void * and provides flexibility in memory management, data abstraction, and type-generic functions.

In this guide, youโ€™ll learn:

  • The concept and syntax of void *
  • How to use void pointers with casting
  • Practical examples in memory handling and libraries
  • Best practices and safety considerations

Core Concept โ€“ Understanding void *

A void * pointer does not have a specific type, so it cannot be dereferenced directly. You must typecast it to a proper pointer type before accessing the value.

Syntax:

void *ptr;

It can point to:

  • int, char, float, struct, or any other type
  • Memory blocks returned by malloc(), calloc()

Code Examples โ€“ Using void Pointers

Example 1: Assigning Different Types

#include <stdio.h>

int main() {
    int a = 10;
    float b = 3.14;
    void *ptr;

    ptr = &a;
    printf("Integer: %d\n", *(int *)ptr);  // Typecast to int *

    ptr = &b;
    printf("Float: %.2f\n", *(float *)ptr);  // Typecast to float *

    return 0;
}

Output:

Integer: 10  
Float: 3.14

Example 2: Use with malloc (Generic Allocation)

#include <stdio.h>
#include <stdlib.h>

int main() {
    void *ptr = malloc(sizeof(int));
    if (ptr != NULL) {
        *(int *)ptr = 100;
        printf("Value: %d\n", *(int *)ptr);
        free(ptr);
    }
    return 0;
}

Useful when creating libraries or allocating memory for unknown types.


Use Cases of void Pointers

Use CaseDescription
Memory Allocationmalloc() returns void *
Type-generic APIsAccept any data type (e.g., qsort(), bsearch())
Callback FunctionsPass void *context to store arbitrary data
Data StructuresUsed in stacks, queues, linked lists, etc.

Best Practices & Tips

Best Practice:
Always cast a void * to the correct type before dereferencing.

Tip:
Use void * in APIs where type flexibility is needed (e.g., utility functions).

Pitfall:
Dereferencing without casting leads to compiler errors or undefined behavior.


Real-World Applications

  • Dynamic memory allocation (malloc, calloc, realloc)
  • Sorting/searching with qsort() and bsearch()
  • Writing type-agnostic data structures
  • Passing arbitrary context to callback functions
  • Generic logging or serialization functions

Summary โ€“ Recap & Next Steps

The void * pointer enables generic programming in C, making it a powerful tool in libraries, memory management, and system APIs.

Key Takeaways:

  • void * can point to any data type
  • Must be cast before dereferencing
  • Used in type-agnostic functions and structures
  • Crucial for flexible and reusable C code

Real-World Relevance:

Used in standard libraries, system calls, embedded systems, and generic data processing frameworks.


Frequently Asked Questions (FAQ)

What is a void * in C?

A generic pointer that can store the address of any data type. Used for flexibility in programming.


Can you dereference a void pointer?

Not directly. You must cast it to the correct type first:

*(int *)ptr

Why does malloc() return a void *?

Because it needs to return memory that can be assigned to any type of pointer:

int *arr = malloc(5 * sizeof(int));

Is void * type-safe?

No. The compiler does not enforce the type of data it points to. You must cast it correctly.


Can I use arithmetic on void pointers?

No. You must cast it to a typed pointer (e.g., int *, char *) before using pointer arithmetic.


Share Now :
Share

๐Ÿ”ณ C void Pointer

Or Copy Link

CONTENTS
Scroll to Top