bsearch.c

来自「与C语言四书五经之《C和指针》的配套的书上源代码。」· C语言 代码 · 共 45 行

C
45
字号
/*
** Demonstrates searching an array of structures with bsearch
*/
#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 ];
	Record	key;
	Record	*ans;

	/*
	** Code that fills the array with 50 elements and sorts it
	*/

	/*
	** Create a key record (only the key field filled in with the
	** value we want to locate) and search the array.
	*/
	strcpy( key.key, "value" );
	ans = bsearch( &key, array, 50, sizeof( Record ),
	    r_compare );

	/*
	** ans now points to the array element whose key field
	** matches the value, or NULL if none matched.
	*/

	return EXIT_SUCCESS;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?