t*********n 发帖数: 278 | 1 the sample from programming pearls. But, I got complier error at this line
qsort(a, n, sizeof(char *), pstrcmp). How can I fix this one? Thanx.
#include
#include
#include
int pstrcmp(char **p, char **q)
{ return strcmp(*p, *q); }
#define MAXN 5000000
char c[MAXN], *a[MAXN];
int main()
{ int i, ch, n = 0, maxi, maxlen = -1;
while ((ch = getchar()) != EOF) {
a[n] = &c[n];
c[n++] = ch;
}
c[n] = 0;
qsort(a, n, sizeof(char *), pstrcmp);
} | P********e 发帖数: 2610 | 2 int pstrcmp(const void *p, const void *q)
{ return strcmp((char*)p, (char*)q); }
google qsort
【在 t*********n 的大作中提到】 : the sample from programming pearls. But, I got complier error at this line : qsort(a, n, sizeof(char *), pstrcmp). How can I fix this one? Thanx. : #include : #include : #include : int pstrcmp(char **p, char **q) : { return strcmp(*p, *q); } : #define MAXN 5000000 : char c[MAXN], *a[MAXN]; : int main()
| r*********r 发帖数: 3195 | 3 the new c compiler is stricter on type checking.
the last argument to qsort function is of type:
int ( * comparator ) ( const void *, const void * )
therefore, you need to cast the pstrcmp to this type. do:
typedef int(*cmp_ptr) (const void *, const void *);
qsort(a, n, sizeof(char *), (cmp_ptr) pstrcmp); | t*********n 发帖数: 278 | 4 many thanks. it works.
【在 r*********r 的大作中提到】 : the new c compiler is stricter on type checking. : the last argument to qsort function is of type: : int ( * comparator ) ( const void *, const void * ) : therefore, you need to cast the pstrcmp to this type. do: : typedef int(*cmp_ptr) (const void *, const void *); : qsort(a, n, sizeof(char *), (cmp_ptr) pstrcmp);
|
|