这是“The C programming language”一书中的C代码:
#include <stdio.h>
#include <string.h>
#define MAXLINES 5000 /* max #lines to be sorted */
char *lineptr[MAXLINES]; /* pointers to text lines */
int readlines(char *lineptr[], int nlines);
void writelines(char *lineptr[], int nlines);
void qsort(void *lineptr[], int left, int right,
int (*comp)(void *, void *));
int numcmp(char *, char *);
/* sort input lines */
main(int argc, char *argv[])
{
int nlines;
int numeric = 0; /* number of input lines read */
if (argc > 1 && strcmp(argv[1], "-n") == 0) /* 1 if numeric sort */
numeric = 1;
if ((nlines = readlines(lineptr, MAXLINES)) >= 0) {
qsort((void**) lineptr, 0, nlines-1, // MY QUESTION: WHY lineptr IS CAST TO POINTER TO A VOID POINTER
(int (*)(void*,void*))(numeric ? numcmp : strcmp));
writelines(lineptr, nlines);
return 0;
} else {
printf("input too big to sort\n");
return 1;
}
}
void qsort(void *v[], int left, int right,
int (*comp)(void *, void *))
{
int i, last;
void swap(void *v[], int, int);
// rest of code
}
为什么当调用 qsort 函数时,第一个参数被强制转换为指向 void 指针 (void **) 而不仅仅是指向 void (void *) 的指针。请告诉我为什么会这样?
谢谢