1.
qsort 头文件需 #include <stdlib.h> #include <search.h>,示例来自MSDN
void qsort( void *base, size_t num, size_t width, int (__cdecl *compare )(const void *, const void *) );
// crt_qsort.c // arguments: every good boy deserves favor /* This program reads the command-line * parameters and uses qsort to sort them. It * then displays the sorted arguments. */ #include <stdlib.h> #include <string.h> #include <stdio.h> int compare( const void *arg1, const void *arg2 ); int main( int argc, char **argv ) { int i; /* Eliminate argv[0] from sort: */ argv++; argc--; /* Sort remaining args using Quicksort algorithm: */ qsort( (void *)argv, (size_t)argc, sizeof( char * ), compare ); /* Output sorted list: */ for( i = 0; i < argc; ++i ) printf( " %s", argv[i] ); printf( "\n" ); } int compare( const void *arg1, const void *arg2 ) { /* Compare all of both strings: */ return _stricmp( * ( char** ) arg1, * ( char** ) arg2 ); }2. qsort_s,安全性比qsort高。头文件需要
<stdlib.h> and <search.h> |
void qsort_s( void *base, size_t num, size_t width, int (__cdecl *compare )(void *, const void *, const void *), void * context ); 示例没看懂