avatar
t*n
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);
}
avatar
P*e
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()

avatar
r*r
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);
avatar
t*n
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);

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。