qsort.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 + -
显示快捷键?