< p >
qsort(3)
库函数的 man 手册给出了一个在命令行参数中排序单词的示例。比较函数如下:
static int
cmpstringp(const void *p1, const void *p2)
{
/* The actual arguments to this function are "pointers to
pointers to char", but strcmp(3) arguments are "pointers
to char", hence the following cast plus dereference */
return strcmp(* (char * const *) p1, * (char * const *) p2);
}
这里正在排序的是argv
的元素。现在argv
是指向指针的字符,也可以看作是指向字符指针的表。
因此,它的元素是指向字符的指针,那么cmpstringp
的实际参数不应该是"指向指针的字符",而是指向字符的指针吗?
const void* p1
转换为int
而不是const int*
? 那么如果是一组10KB的结构体数组呢?它们的元素比void*更大,这种情况下该怎么做? - Pete Kirkham