qsort(3)库例程的联机帮助页给出了在命令行上对作为参数给出的单词进行排序的示例.比较函数如下:
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);
}
Run Code Online (Sandbox Code Playgroud)
但是这里排序的是元素argv.现在argv是指向字符指针的指针,它也可以被视为指向字符的指针表.
因此它的元素是指向字符的指针,所以不应该是指向字符的实际参数cmpstringp,而不是"指向char的指针"?
作为参数传递的回调函数qsort()被调用,作为参数,指向要比较的两个值的指针.如果你排序一个char *(例如argv[])数组,那么值是char *(指向char),比较函数将接收指向这些值的指针,即指向指针的指针char.