Hacker News new | ask | show | jobs
by teo_zero 21 days ago
Yes, some restrictions seem arbitrary. Just like why these two types are not compatible:

  struct a {int data;};
  struct b {int data;};
I know, I know, changing it would break existing code, etc.
1 comments

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?