R
Richard Heathfield
santosh said:
No. The Standard defines the semantics (so much so that, if you provide
different semantics, the program's behaviour is undefined):
"The contents of the array are sorted in ascending order according
to a comparison function pointed to by compar , which is called with
two arguments that point to the objects being compared. The function
shall return an integer less than, equal to, or greater than zero if
the first argument is considered to be respectively less than, equal
to, or greater than the second."
Thus, the following function is non-standard:
int cmpint(const void *vp, const void *vq)
{
const int *p = vp;
const int *q = vq;
return (*p < *q) - (*p > *q);
}
....but if your program passes it to qsort, it has defined semantics which
it must not violate (or, rather, shall not violate).
Doesn't that constitute as just defining the interface?
No. The Standard defines the semantics (so much so that, if you provide
different semantics, the program's behaviour is undefined):
"The contents of the array are sorted in ascending order according
to a comparison function pointed to by compar , which is called with
two arguments that point to the objects being compared. The function
shall return an integer less than, equal to, or greater than zero if
the first argument is considered to be respectively less than, equal
to, or greater than the second."
Thus, the following function is non-standard:
int cmpint(const void *vp, const void *vq)
{
const int *p = vp;
const int *q = vq;
return (*p < *q) - (*p > *q);
}
....but if your program passes it to qsort, it has defined semantics which
it must not violate (or, rather, shall not violate).