e******d 发帖数: 310 | 1 I want to sort strings using qsort(), and try the following code, but doesn
't work. Could you please help me out. Thank you.
=============================================
#include "stdlib.h"
#include "stdio.h"
#include "string.h"
int mystrcmp(const void * str1, const void * str2)
{
return strcmp( (const char*)str1, (const char*)str2 ) ;
}
int main()
{
int n = 4;
char *pstr[] = {"God", "Bed", "Act", "Cup"};
qsort(pstr, n, sizeof(char*), mystrcmp);
int i = 0;
for(i = 0; | X****r 发帖数: 3557 | 2 int mystrcmp(const void * str1, const void * str2)
{
return strcmp(*(const char* const*)str1, *(const char* const*)str2);
}
doesn
【在 e******d 的大作中提到】 : I want to sort strings using qsort(), and try the following code, but doesn : 't work. Could you please help me out. Thank you. : ============================================= : #include "stdlib.h" : #include "stdio.h" : #include "string.h" : int mystrcmp(const void * str1, const void * str2) : { : return strcmp( (const char*)str1, (const char*)str2 ) ; : }
| z****e 发帖数: 2024 | 3 C还很流行吗?
还是都是C++的天下了?
【在 X****r 的大作中提到】 : int mystrcmp(const void * str1, const void * str2) : { : return strcmp(*(const char* const*)str1, *(const char* const*)str2); : } : : doesn
| p***o 发帖数: 1252 | 4 It really doesn't matter which language you are using.
Actually qsort is a good example to show someone familiar with C
the idea of functor in C++. While C++ automatically matches
(or finds a mismatch of) the types for std::sort, in C you have
to do it by hand and be very careful; otherwise you will get
the same error as LZ.
【在 z****e 的大作中提到】 : C还很流行吗? : 还是都是C++的天下了?
| e**********n 发帖数: 359 | 5 Can you just use *(char **)str1 and *(char **)str2 ?
【在 X****r 的大作中提到】 : int mystrcmp(const void * str1, const void * str2) : { : return strcmp(*(const char* const*)str1, *(const char* const*)str2); : } : : doesn
| X****r 发帖数: 3557 | 6 Yes, in this case they are the same in result.
But generally speaking, const should be used wherever applicable
to help prevent programming errors.
【在 e**********n 的大作中提到】 : Can you just use *(char **)str1 and *(char **)str2 ?
| c*******9 发帖数: 6411 | 7 Xentar,
could you explain why the correct function is :
int mystrcmp(const void * str1, const void * str2)
{
return strcmp(*(const char* const*)str1, *(const char* const*)str2);
}
and not:
nt mystrcmp(const void * str1, const void * str2)
{
return strcmp( (const char*)str1, (const char*)str2 ) ;
}
The array elements here are pointer to char*?
Thanks for the help! | X****r 发帖数: 3557 | 8 qsort passes pointers to the objects being compared. Your objects
in the array are "char *", thus the comparison function parameters
have actual type "char **".
For example, if you're sorting integers, you would do:
int myintcmp(const void *p1, const void *p2) {
return *(const int*)p1 - *(const int *)p2;
}
int data[] = {1,3,2,4};
qsort(data, sizeof(data)/sizeof(data[0]), sizeof(data[0]), myintcmp);
Just replace "int" in above example with "char *" you'll see.
const*)str2);
【在 c*******9 的大作中提到】 : Xentar, : could you explain why the correct function is : : int mystrcmp(const void * str1, const void * str2) : { : return strcmp(*(const char* const*)str1, *(const char* const*)str2); : } : and not: : nt mystrcmp(const void * str1, const void * str2) : { : return strcmp( (const char*)str1, (const char*)str2 ) ;
| c*******9 发帖数: 6411 | |
|