Skip to main content

Command Palette

Search for a command to run...

Void pointers

Updated
2 min read
J

I am Jyotiprakash, a deeply driven computer systems engineer, software developer, teacher, and philosopher. With a decade of professional experience, I have contributed to various cutting-edge software products in network security, mobile apps, and healthcare software at renowned companies like Oracle, Yahoo, and Epic. My academic journey has taken me to prestigious institutions such as the University of Wisconsin-Madison and BITS Pilani in India, where I consistently ranked among the top of my class.

At my core, I am a computer enthusiast with a profound interest in understanding the intricacies of computer programming. My skills are not limited to application programming in Java; I have also delved deeply into computer hardware, learning about various architectures, low-level assembly programming, Linux kernel implementation, and writing device drivers. The contributions of Linus Torvalds, Ken Thompson, and Dennis Ritchie—who revolutionized the computer industry—inspire me. I believe that real contributions to computer science are made by mastering all levels of abstraction and understanding systems inside out.

In addition to my professional pursuits, I am passionate about teaching and sharing knowledge. I have spent two years as a teaching assistant at UW Madison, where I taught complex concepts in operating systems, computer graphics, and data structures to both graduate and undergraduate students. Currently, I am an assistant professor at KIIT, Bhubaneswar, where I continue to teach computer science to undergraduate and graduate students. I am also working on writing a few free books on systems programming, as I believe in freely sharing knowledge to empower others.

Void pointers in C are a type of pointer that can point to any data type. They are used for generic type handling in C programming. Here's a breakdown of their characteristics, uses, and pitfalls:

Characteristics of Void Pointers

  1. Typeless: Void pointers do not have a data type associated with them. They can point to any data type, from an integer to a structure.

  2. Memory Allocation: They are often used with memory allocation functions like malloc() and free(), which return and accept void pointers, respectively.

Uses of Void Pointers

  1. Generic Functions: They allow functions to be more generic. For example, the qsort() function in C uses void pointers to sort different data types.

  2. Memory Functions: Functions like memcpy() use void pointers to copy any type of data.

Generic Sorting Function

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

// Comparison function for integers
int compareInts(const void* a, const void* b) {
    return (*(int*)a - *(int*)b);
}

// Comparison function for floats
int compareFloats(const void* a, const void* b) {
    float diff = *(float*)a - *(float*)b;
    return (diff > 0) - (diff < 0);
}

// Generic sort function
void sort(void* array, size_t nItems, size_t size, int (*compare)(const void*, const void*)) {
    qsort(array, nItems, size, compare);
}

int main() {
    int intArr[] = {5, 1, 4, 2, 3};
    float floatArr[] = {2.5, 1.1, 3.7, 0.4, 2.0};

    size_t nIntItems = sizeof(intArr) / sizeof(intArr[0]);
    size_t nFloatItems = sizeof(floatArr) / sizeof(floatArr[0]);

    // Sorting integers
    sort(intArr, nIntItems, sizeof(intArr[0]), compareInts);
    printf("Sorted Integers: ");
    for(size_t i = 0; i < nIntItems; i++)
        printf("%d ", intArr[i]);
    printf("\n");

    // Sorting floats
    sort(floatArr, nFloatItems, sizeof(floatArr[0]), compareFloats);
    printf("Sorted Floats: ");
    for(size_t i = 0; i < nFloatItems; i++)
        printf("%.1f ", floatArr[i]);
    printf("\n");

    return 0;
}

Explanation:

    • Two comparison functions are defined: compareInts for integers and compareFloats for floats. The float comparison handles the possibility of floating-point imprecision.

      • The sort function is called twice: once for sorting an array of integers (intArr) and once for sorting an array of floats (floatArr).

      • The program demonstrates how void pointers in the sort function can be utilized to sort arrays of different data types using the appropriate comparison functions.

Pitfalls of Using Void Pointers

  1. Type Safety: Void pointers provide no information about the type of data they point to. This can lead to runtime errors if the pointer is incorrectly cast to the wrong type.

  2. Lack of Operations: You cannot directly dereference or perform arithmetic on void pointers without casting them to another type.

  3. Debugging Difficulty: Errors involving void pointers can be harder to diagnose because the compiler cannot check the types during compilation.

More from this blog

Jyotiprakash's Blog

251 posts

I'm Jyotiprakash, a software dev and professor at KIIT, with expertise in system programming.