C Programs Tutorials | IT Developer
IT Developer

C Programming - C void Pointer



Share with a Friend

C Programming - C void Pointer

C void Pointer

A void pointer in C is a special type of pointer that can point to any data type. It is a generic pointer, meaning it can store the address of any variable, regardless of its data type (integer, float, structure, etc.). The void pointer is declared with the void keyword and is often used in situations where the type of the data being pointed to is unknown or can vary.

  1. Declaration of Void Pointer:

A void pointer is declared using the void keyword followed by the pointer asterisk *.

C

void *ptr;

Here, ptr is a void pointer that can point to any data type.

  1. Assigning Values to Void Pointer:

A void pointer can be assigned the address of any data type variable. However, since the compiler doesn't know the type of data being pointed to, it cannot directly dereference a void pointer. The compiler requires the type to be specified for dereferencing.

C

#include <stdio.h>

int main() {

    int num = 10;

    float decimal = 3.14;

    // Declaring void pointers

    void *ptr;

    // Assigning the address of an integer variable

    ptr = &num;

    printf("Integer value: %d\n", *(int*)ptr);  // Typecast to dereference as an integer

    // Assigning the address of a float variable

    ptr = &decimal;

    printf("Float value: %.2f\n", *(float*)ptr);  // Typecast to dereference as a float

    return 0;

}

Output:

Integer value: 10

Float value: 3.14

In this example:

  • ptr is a void pointer.
  • It is first assigned the address of an integer variable num and typecasted to (int*) to dereference it.
  • Then, it is assigned the address of a float variable decimal and typecasted to (float*) to dereference it.
  1. Void Pointer Usage:
  • Generic functions: Void pointers are commonly used in generic functions or functions that need to handle different data types. For example, functions that take pointers to data structures or memory locations but are not concerned with the type of the data they are dealing with.
  • Memory allocation functions: Functions like malloc() and calloc() return void* because they can allocate memory for any data type.

C

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

  • Handling different data types in a single function: When you want to write a function that works with multiple types of data, you can pass a void pointer and use type casting to manipulate the data.
  1. Typecasting a Void Pointer:

Since the void pointer is not associated with a specific data type, you need to typecast it to the appropriate data type before dereferencing it.

C

#include <stdio.h>

void printData(void *ptr, char type) {

    if (type == 'i') {

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

    } else if (type == 'f') {

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

    }

}

int main() {

    int num = 5;

    float decimal = 7.5;

    printData(&num, 'i');  // Pass integer

    printData(&decimal, 'f');  // Pass float

    return 0;

}

Output:

Integer: 5

Float: 7.50

Here, the function printData uses a void pointer to handle different data types (int and float) and uses typecasting to access the correct data.

  1. Void Pointer Limitations:
  • Cannot be dereferenced directly: You cannot dereference a void pointer directly because the compiler doesn't know the type of data it points to. You must first typecast it to the appropriate type.
  • Memory allocation: Functions like malloc() and calloc() return void pointers, but when using the allocated memory, you need to cast the void pointer to a specific type.
  1. Void Pointer with Arrays:

A void pointer can also be used with arrays. Since arrays are just pointers, a void pointer can point to the base address of any array.

C

#include <stdio.h>

void printArray(void *arr, int size, char type) {

    if (type == 'i') {

        int *intArr = (int*)arr;

        for (int i = 0; i < size; i++) {

            printf("%d ", intArr[i]);

        }

        printf("\n");

    } else if (type == 'f') {

        float *floatArr = (float*)arr;

        for (int i = 0; i < size; i++) {

            printf("%.2f ", floatArr[i]);

        }

        printf("\n");

    }

}

int main() {

    int intArr[] = {1, 2, 3, 4, 5};

    float floatArr[] = {3.14, 2.71, 1.41};

    printArray(intArr, 5, 'i');

    printArray(floatArr, 3, 'f');

    return 0;

}

Output:

1 2 3 4 5

3.14 2.71 1.41

In this case, the void pointer is used to point to arrays of different data types (int and float), and typecasting is applied to properly access the array elements.

Key Takeaways:

  • A void pointer is a generic pointer type in C that can point to any data type.
  • You must typecast a void pointer to the appropriate type before dereferencing it.
  • Common uses of void pointers include generic functions, memory allocation, and handling different data types.
  • Memory allocation functions like malloc() and calloc() return void pointers.
  • Void pointers provide flexibility but require careful management and typecasting to ensure correct data handling.

Using void pointers enhances the flexibility and reusability of functions, especially in cases where you need to handle multiple data types. However, you must exercise caution and ensure proper typecasting to avoid errors.