qsort.c
来自「C和指针源程序 C和指针源程序C和指针源程序C和指针源程序」· C语言 代码 · 共 36 行
C
36 行
/*
** Demonstrates sorting an array of structures with qsort
*/
#include <stdlib.h>
#include <string.h>
typedef struct {
char key[ 10 ]; /* the sort key for the array */
int other_data; /* data associated with the key */
} Record;
/*
** Comparison function: compares only the key value.
*/
int r_compare( void const *a, void const *b ){
return strcmp( ((Record *)a)->key, ((Record *)b)->key );
}
int
main()
{
Record array[ 50 ];
/*
** Code that fills the array with 50 elements.
*/
qsort( array, 50, sizeof( Record ), r_compare );
/*
** Array is now sorted by the key field of the structures.
*/
return EXIT_SUCCESS;
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?