qsort()给字符串排序问题
#include<stdlib.h>
#include<stdio.h>
static int comp(const void *a, const void *b)
{
return strcmp( * (const char ** )a,* (const char **) b);
}
void sortStrings(const char * array[])
{
int num;
for (num=0; array[num]; num++){};
qsort(array, num, sizeof( * array), comp) ;
}
main()
{
int i;
char *string[4]={"boy","girl","apple","dog"};
sortStrings(string);/*这行有错误*/
for(i=0;i<4;i++)printf("%d",string[i]);
system("Pause");
}
源代码如上,提示错误: [Warning] passing arg 1 of `sortStrings' from incompatible pointer type ,不知道为什么错?
#include<stdio.h>
static int comp(const void *a, const void *b)
{
return strcmp( * (const char ** )a,* (const char **) b);
}
void sortStrings(const char * array[])
{
int num;
for (num=0; array[num]; num++){};
qsort(array, num, sizeof( * array), comp) ;
}
main()
{
int i;
char *string[4]={"boy","girl","apple","dog"};
sortStrings(string);/*这行有错误*/
for(i=0;i<4;i++)printf("%d",string[i]);
system("Pause");
}
源代码如上,提示错误: [Warning] passing arg 1 of `sortStrings' from incompatible pointer type ,不知道为什么错?
...全文
相关推荐
sort()与qsort()排序详解[源码]
在效率方面,两者都通过内部优化实现了较高的排序速度,但sort()可能在处理STL容器时更为高效。稳定性方面,sort()通常提供稳定的排序结果,而qsort()则可能导致稳定性问题。
qsort 各种用法
/* 初始化数组 */ }; qsort(s, 100, sizeof(s[0]), cmp_info2);}```##### 六、对字符串进行排序字符串排序通常基于字典序,可利用`strcmp`函数实现
C++中的Qsort
### C++中的Qsort与Sort详解在C++编程领域,`qsort()`与`sort()`函数是处理数据排序的两个重要工具。
七种qsort排序方法
#### 六、对字符串进行排序对于结构体中的字符串进行排序,可以使用标准库函数 `strcmp`。
C中qsort快速排序使用实例
对于字符串数组的排序,每个元素是字符串的指针,因此在`compare`函数中需要将`void *`转换为`char **`,这样可以访问到字符串的实际内容,并使用`strcmp`函数进行比较。


