← Back to context

Comment by amiga386

5 months ago

You jest, but a central feature of C is that void* is a universal pointer, and can be used as the basis of polymorphism and context passing. That's why you can do things like:

    void qsort(void *base, size_t nmemb, size_t size,
               int (*compar)(const void *, const void *));

It would be nice to write:

    int compare_foo(const struct foo *a, const struct foo *b) {
        ...
    }

But instead we have to write this to avoid being declared "undefined behaviour":

    int compare_foo(const void *a, const void *b) {
        const struct foo *real_a = (struct foo *)a;
        const struct foo *real_b = (struct foo *)b;
        ...
    }

So what is the upside of having this rule, given the same thing is going to happen anyway, just more verbose?