This repository has been archived on 2024-12-16. You can view files and clone it, but cannot push or open issues or pull requests.
CodeBlocksPortable/WATCOM/samples/clibexam/bsrch_s.c

43 lines
956 B
C

#define __STDC_WANT_LIB_EXT1__ 1
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static const char *keywords[] = {
"auto",
"break",
"case",
"char",
/* . */
/* . */
/* . */
"while"
};
static void * context = NULL;
#define NUM_KW sizeof(keywords) / sizeof(char *)
int kw_compare( const void *p1, const void *p2, void *context )
{
const char *p1c = (const char *) p1;
const char **p2c = (const char **) p2;
return( strcmp( p1c, *p2c ) );
}
int keyword_lookup( const char *name )
{
const char **key;
key = (char const **) bsearch_s( name, keywords, NUM_KW,
sizeof( char * ), kw_compare, context );
if( key == NULL ) return( -1 );
return key - keywords;
}
int main()
{
printf( "%d\n", keyword_lookup( "case" ) );
printf( "%d\n", keyword_lookup( "crigger" ) );
printf( "%d\n", keyword_lookup( "auto" ) );
return 0;
}