top of page

Void pointers

  • Writer: Siddharth Sharma
    Siddharth Sharma
  • Nov 7, 2024
  • 2 min read

A void pointer is a special type of pointer in C and C++ that can point to any data type. Declared as void*, it is a generic pointer because it has no specific data type associated with it. This flexibility makes void pointers useful for creating generic functions and structures, where the type of data being pointed to may vary.


Key Characteristics of Void Pointers

  • No Type Association: A void pointer (void*) does not have a specific type, so it can hold the address of any data type (e.g., int, float, char, or even structs).

  • Typecasting Requirement: Since a void pointer does not have a data type, you cannot dereference it directly. You must first typecast it to the appropriate type before dereferencing.

  • Usefulness in Generic Programming: Void pointers are often used for generic functions (like memory allocation) and data structures that need to handle multiple types of data.


Declaring a Void Pointer

Here’s how to declare a void pointer:


void* ptr;

Assigning a Void Pointer to Different Data Types

A void pointer can hold the address of any data type. Here’s an example showing how to assign a void pointer to different types and then access the values:


#include <stdio.h>

int main() {
    int num = 42;
    float fnum = 3.14;
    char ch = 'A';

    void* ptr;  // Declare a void pointer

    // Point to an integer
    ptr = &num;
    printf("Value of num through void pointer: %d\n", *((int*)ptr));  // Typecast and dereference

    // Point to a float
    ptr = &fnum;
    printf("Value of fnum through void pointer: %.2f\n", *((float*)ptr));  // Typecast and dereference

    // Point to a char
    ptr = &ch;
    printf("Value of ch through void pointer: %c\n", *((char*)ptr));  // Typecast and dereference

    return 0;
}

Explanation of the Code:

  1. void* ptr; declares a void pointer ptr.

  2. Point to int: ptr = &num; assigns the address of num (an int) to ptr. Then ((int)ptr) typecasts and dereferences it to retrieve num's value.

  3. Point to float: ptr = &fnum; assigns the address of fnum (a float) to ptr. ((float)ptr) typecasts and dereferences it to retrieve fnum's value.

  4. Point to char: ptr = &ch; assigns the address of ch (a char) to ptr. ((char)ptr) typecasts and dereferences it to retrieve ch's value.


Output:

Value of num through void pointer: 42
Value of fnum through void pointer: 3.14
Value of ch through void pointer: A

Use Cases of Void Pointers

  1. Generic Functions: Functions like malloc() and free() in C use void pointers because they can allocate memory for any data type.

int* ptr = (int*) malloc(sizeof(int) * 5);  // malloc returns a void pointer

  1. Data Structures: In some data structures (e.g., linked lists or stacks), a void pointer can be used as a generic container to store various data types.

  2. Function Parameters: Void pointers are sometimes used in function parameters to allow a function to accept data of any type.


Summary

  • Void pointers are versatile pointers that can point to any data type.

  • To dereference a void pointer, you must first typecast it to the appropriate pointer type.

  • Void pointers enable generic programming by allowing code that can handle multiple types of data.

 
 
 

Comments


bottom of page