【发布时间】:2012-10-24 21:35:24
【问题描述】:
当我学习使用 qsort 对字符串数组进行排序时,有一个问题让我很困惑。
比如对下面的s进行排序
char *s[] = {
"Amit",
"Garima",
"Gaurav",
"Vaibhav"
};
要使用 qsort,您必须提供一个比较函数,例如
以下函数cstring_cmp 我猜在qsort 函数中,要传递给函数cstring_cmp 的参数类型是char**。如何将char** 转换为void*?为什么我们可以将char** 转换为void*?
int cstring_cmp(const void *a, const void *b)
{
const char **ia = (const char **)a;
const char **ib = (const char **)b;
return -strcasecmp(*ia, *ib);
/* return the negative of the normal comparison */
}
【问题讨论】:
-
qsort() 在这个例子中将传递每个被检查的 char * 的 address。它们是指向指针的指针。这正是它应该的样子(在这个例子中也是如此)。 Greg 你没有错(除非我们都错了,但从 OPs 代码来看,我们没有错)。
标签: c void-pointers