-
lsearch / lfindSystem Programming 2007. 11. 1. 16:44728x90#include <stdio.h>
#include <search.h>
#define TABLESIZE 5
int compare(const void *ap, const void *bp)
{
return ( *(int *)ap - *(int *)bp);
}
void main()
{
int table[TABLESIZE]={1,2,3,4,5};
unsigned int n = TABLESIZE;
int item, *ptr;
item = 6;
ptr = (int*)lsearch(&item, table, &n, sizeof(int), compare);
if (ptr >= table +n-1)
printf("%d is not in the table(1-%d), but added.\n",item,n);
else
printf("%d in in the table(1-%d).\n",*ptr,n);
item =7;
ptr = (int*)lfind(&item, table, &n, sizeof(int), compare);
if (ptr == NULL)
printf("%d is not in the table(1-%d).\n",item,n);
else
printf("%d in in the table(1-%d).\n",*ptr,n);
}'System Programming' 카테고리의 다른 글
qsort (0) 2007.11.01 프로그램 옵션 처리 예제 (0) 2007.11.01 리눅스 로그인 사용자 명령어 (1) 2007.09.29 select shell script (0) 2007.09.20 실습 보고서 작성 (0) 2007.09.06