void pointer or generic pointer in C – use and arithmetic

Pointer is a variable pointing at a memory location of specific type. Type defines many important properties related to the pointer. Such as valid memory addresses it can point, pointer arithmetic, etc.

As per C programming semantics, you must specify pointer type during its declaration. Also, it is illegal to point pointer of one type to object of another type. For example, int pointer cannot point to a float variable.

Type specific pointers are beneficial in different ways. However, in programming there happens situation when you need to go typeless. For example, suppose you are required to design a function that can accept any type value as argument, process data and return results. For such situation, you need a pointer that must work with all types.

What is a void pointer?

A void pointer is a special pointer that can point to object of any type. A void pointer is typeless pointer also known as generic pointer. void pointer is an approach towards generic functions and generic programming in C.

Note: Writing programs without being constrained by data type is known as generic programming. A generic function is a special function that focuses on logic without confining to data type. For example, logic to insert values in array is common for all types and hence can be transformed to generic function.

Syntax to declare void pointer

void * pointer-name;

Example to declare void pointer

void * vPtr;

How to dereference a void pointer

Dereferencing is the process of retrieving data from memory location pointed by a pointer. It converts block of raw memory bytes to a meaningful data (data is meaningful if type is associated).

While dereferencing a void pointer, the C compiler does not have any clue about type of value pointed by the void pointer. Hence, dereferencing a void pointer is illegal in C. But, a pointer will become useless if you cannot dereference it back.

To dereference a void pointer you must typecast it to a valid pointer type.

Example to dereference a void pointer

int num = 10;
void * vPtr = #  // void pointer pointing at num

int value = *((int *) vPtr); // Dereferencing void pointer

void pointer arithmetic

void pointer arithmetic is illegal in C programming, due to the absence of type. However, some compiler supports void pointer arithmetic by assuming it as a char pointer.

To perform pointer arithmetic on void pointer you must first typecast to other type.

Example of void pointer arithmetic

int arr[] = {10, 20, 30, 40, 50};
void * vPtr = &arr;  // void pointer pointing at arr

vPtr = ((int *) vPtr + 1);  // add 1 to void pointer

Example program to use void pointer

Write a C function to accept an array and print its elements. The function must accept array of different types.

/**
 * C program to demonstrate void pointer
 */

#include <stdio.h>
#define SIZE 10


/* Function declaration */
void printArray(void * vPtr, int size, int type);


int main()
{
    int num[SIZE] = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100};
    float fractional[SIZE] = {1.1f, 1.2f, 1.3f, 1.4f, 1.5f, 1.6f, 1.7f, 1.8f, 1.9f, 2.0f};
    char characters[SIZE] = {'C', 'o', 'd', 'e', 'f', 'o', 'r', 'w', 'i', 'n'};

    printf("\nElements of integer array: ");
    printArray(&num, SIZE, 1);

    printf("\nElements of float array: ");
    printArray(&fractional, SIZE, 2);

    printf("\nElements of character array: ");
    printArray(&characters, SIZE, 3);

    return 0;
}



/**
 * Function to print array of different types.
 *
 * @vPtr  Pointer to an array
 * @size  Size of the array
 * @type  Integer value specifying type of array. 
 *        1 - Integer, 
 *        2 - Float, 
 *        3 - Character
 */
void printArray(void * vPtr, int size, int type)
{
    int i;
    
    for(i=0; i<size; i++)
    {
        // Print array elements based on their type
        switch(type)
        {
            case 1: 
                /* Typecast void pointer to integer then print */
                printf("%d, ", *((int *)vPtr + i));
            break;

            case 2: 
                /* Typecast void pointer to float then print */
                printf("%f, ", *((float *)vPtr + i));
            break;

            case 3: 
                /* Typecast void pointer to char then print */
                printf("%c, ", *((char *)vPtr + i));
            break;
        }
    }
}

Output

Elements of integer array: 10, 20, 30, 40, 50, 60, 70, 80, 90, 100,
Elements of float array: 1.100000, 1.200000, 1.300000, 1.400000, 1.500000, 1.600000, 1.700000, 1.800000, 1.900000, 2.000000,
Elements of character array: C, o, d, e, f, o, r, w, i, n,

Write your doubts or suggestion. I will try my best to help. You must escape source code before commenting. To format your source code paste your source code inside
<pre><code> ----Your Source Code---- </code></pre>