由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - code question
相关主题
EOF一问c++ string 一问
请教一个字符串比较排序的问题C++ STL map find does not work ???
请教如何使用qsort() to sort string.C++: use char * in Map
nv的显卡能战胜intel的CPU么c字符串内存分配问题
c++ 问题 (转载)帮忙看看这几段程序有问题吗?
大家看看这个简单的qsort排序的问题问个algorithms in C的mergesort问题?
A question about cost char*Array in C
数组指针的问题一个读用户输入的小问题
相关话题的讨论汇总
话题: pstrcmp话题: char话题: maxn话题: qsort话题: int
进入Programming版参与讨论
1 (共1页)
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);

1 (共1页)
进入Programming版参与讨论
相关主题
一个读用户输入的小问题c++ 问题 (转载)
[合集] how to `pause' in C programming大家看看这个简单的qsort排序的问题
请教Visual C++ Express Edition 2008的console windowA question about cost char*
C ++ 问题数组指针的问题
EOF一问c++ string 一问
请教一个字符串比较排序的问题C++ STL map find does not work ???
请教如何使用qsort() to sort string.C++: use char * in Map
nv的显卡能战胜intel的CPU么c字符串内存分配问题
相关话题的讨论汇总
话题: pstrcmp话题: char话题: maxn话题: qsort话题: int