比如说我这个程序:
int own_compare(char a[],char b[])
{
return(strcmp(a,b));//这样写会报错。。。
}
int main()
{
char t[10][20]={"abcd","sdfghjkqwer","dfghjkqwer","fghjkqwer","ghjkqwer","hjkqwer","jkqwer","kqwer","qwer","wer"};
qsort(t,10,sizeof(t[0]),own_compare);
for(int i=0;i<10;i++)
{
puts(t[i]);
}
getchar();
}
3 个解决方案
#1
char str[100];形参类型不对应
}s[100];
int own_compare(const void *a,const void *b)
{
return strcmp(((In *)a)->str,((In *)b)->str);
}
int main()
{
char t[10][20]={"abcd","sdfghjkqwer","dfghjkqwer","fghjkqwer","ghjkqwer","hjkqwer","jkqwer","kqwer","qwer","wer"};
qsort(t,10,sizeof(t[0]),own_compare);
for(int i=0;i<10;i++)
{
puts(t[i]);
}
getchar();
}
#2
void qsort(void *base, size_t n, size_t size,
int (*cmp)(const void *, const void *))
qsort sorts into ascending order an array base[0]...base[n-1] of objects of size size. The comparison function cmp is as in bsearch.
int (*cmp)(const void *, const void *))
qsort sorts into ascending order an array base[0]...base[n-1] of objects of size size. The comparison function cmp is as in bsearch.
#3
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int own_compare(const void *v1, const void *v2)
{
const char *p1, *p2;
p1 = (const char *)v1;
p2 = (const char *)v2;
return strcmp(p1, p2);
}
int main(int argc, char *argv[])
{
int i;
char t[10][20]={"abcd","sdfghjkqwer","dfghjkqwer","fghjkqwer","ghjkqwer","hjkqwer","jkqwer","kqwer","qwer","wer"};
qsort(t,10,sizeof(t[0]),own_compare);
for(i=0;i<10;i++)
puts(t[i]);
return 0;
}
#1
char str[100];形参类型不对应
}s[100];
int own_compare(const void *a,const void *b)
{
return strcmp(((In *)a)->str,((In *)b)->str);
}
int main()
{
char t[10][20]={"abcd","sdfghjkqwer","dfghjkqwer","fghjkqwer","ghjkqwer","hjkqwer","jkqwer","kqwer","qwer","wer"};
qsort(t,10,sizeof(t[0]),own_compare);
for(int i=0;i<10;i++)
{
puts(t[i]);
}
getchar();
}
#2
void qsort(void *base, size_t n, size_t size,
int (*cmp)(const void *, const void *))
qsort sorts into ascending order an array base[0]...base[n-1] of objects of size size. The comparison function cmp is as in bsearch.
int (*cmp)(const void *, const void *))
qsort sorts into ascending order an array base[0]...base[n-1] of objects of size size. The comparison function cmp is as in bsearch.
#3
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int own_compare(const void *v1, const void *v2)
{
const char *p1, *p2;
p1 = (const char *)v1;
p2 = (const char *)v2;
return strcmp(p1, p2);
}
int main(int argc, char *argv[])
{
int i;
char t[10][20]={"abcd","sdfghjkqwer","dfghjkqwer","fghjkqwer","ghjkqwer","hjkqwer","jkqwer","kqwer","qwer","wer"};
qsort(t,10,sizeof(t[0]),own_compare);
for(i=0;i<10;i++)
puts(t[i]);
return 0;
}