I was reading the GLibC reference on the searching and sorting functions here and here and in one of the code examples, typecasting is used while in the other, it is not:
First one:
int
compare_doubles (const void *a, const void *b)
{
const double *da = (const double *) a;
const double *db = (const double *) b;
return (*da > *db) - (*da < *db);
}
Second one:
int
critter_cmp (const void *v1, const void *v2)
{
const struct critter *c1 = v1;
const struct critter *c2 = v2;
return strcmp (c1->name, c2->name);
}
Is there any reason why in the second example, something like
const struct critter *c1 = (const struct critter *)v1;
was not used? or was this done only for brevity?
Edit: Is casting in the first example necessary then if the compiler can deduce it? Are there any best practices for this kind of thing?