## lsearch ##
線性搜索
### 相關函數 ###
lfind
### 表頭文件 ###
```
#include<stdlib.h>
```
### 定義函數 ###
```
void *lsearch(const void * key, const void *base, size_t *nmemb,
size_t size, int (*compar)(const void *, const void *));
```
### 函數說明 ###
lsearch()利用線性搜索在數組中從頭至尾一項項查找數據。參數key指向欲查找的關鍵數據,參數base指向要被搜索的數組開頭地址,參數nmemb 代表數組中的元素數量,每一元素的大小則由參數size 決定,最后一項參數compar 為一函數指針,這個函數用來判斷兩個元素是否相同,若傳給compar 的第一個參數所指的元素數據和第二個參數所指的元素數據相同時則返回0,兩個元素數據不相同則返回非0 值。如果lsearch()找不到關鍵數據時會主動把該項數據加入數組里。
### 返回值 ###
找到關鍵數據則返回找到的該筆元素的四肢,如果在數組中找不到關鍵數據則將此關鍵數據加入數組,再把加入數組后的地址返回。
### 范例 ###
```
#include<stdio.h>
#include<stdlib.h>
#define NMEMB 50
#define SIZE 10
int compar (comst void *a,const void *b)
{
return (strcmp((char *) a, (char *) b));
}
main()
{
char data[NMEMB][SIZE]={“Linux”,”freebsd”,”solzris”,”sunos”,”windows”};
char key[80],*base,*offset;
int i, nmemb=NMEMB,size=SIZE;
for(i=1;i<5;i++){
fgets(key,sizeof9key),stdin);
key[strlen(key)-1]=’\0’;
base = data[0];
offset = (char *)lfind(key,base,&nmemb,size,compar);
if(offset ==NULL){
printf(“%s not found!\n”,key);
offset=(char *) lsearch(key,base,&nmemb,size,compar);
printf(“Add %s to data array\n”,offset);
}else{
printf(“found : %s \n”,offset);
}
}
}
```
### 執行 ###
```
linux
found:linux
os/2
os/2 not found!
add os/2 to data array
os/2
found:os/2
```
- 字符測試篇
- isalnum
- isalpha
- isascii
- iscntrl
- isdigit
- isgraphis
- islower
- isprint
- isspace
- ispunct
- isupper
- isxdigit
- 字符串轉換篇
- atof
- atoi
- atol
- gcvt
- strtod
- strtol
- strtoul
- toascii
- tolower
- toupper
- 內存控制篇
- calloc
- free
- getpagesize
- malloc
- mmap
- munmap
- 日期時間篇
- asctime
- ctime
- gettimeofday
- gmtime
- localtime
- mktime
- settimeofday
- time
- 內存及字符串操作篇
- bcmp
- bcopy
- bzero
- index
- memccpy
- memchr
- memcmp
- memcpy
- memmove
- memset
- rindex
- strcasecmp
- strcat
- strchr
- strcmp
- strcoll
- strcpy
- strcspn
- strdup
- strlen
- strncasecmp
- strncat
- strncpy
- strpbrk
- strrchr
- strspn
- strstr
- strtok
- 常用數學函數篇
- abs
- acos
- asin
- atan
- atan2
- ceil
- cos
- cosh
- exp
- frexp
- ldexp
- log
- log10
- pow
- sin
- sinh
- sqrt
- tan
- tanh
- 用戶組篇
- endgrent
- endpwent
- endutent
- fgetgrent
- fgetpwent
- getegid
- geteuid
- getgid
- getgrent
- getgrgid
- getgrnam
- getgroups
- getpw
- getpwent
- getpwnam
- getpwuid
- getuid
- getutent
- getutid
- getutline
- initgroups
- pututline
- seteuid
- setfsgid
- setfsuid
- setgid
- setgrent
- setgroups
- setpwent
- setregid
- setreuid
- setuid
- setutent
- utmpname
- 數據結構及算法篇
- crypt
- bsearch
- lfind
- lsearch
- qsort
- rand
- srand
- 文件操作篇
- close
- creat
- dup
- dup2
- fcntl
- flock
- fsync
- lseek
- mkstemp
- open
- read
- sync
- write
- 文件內容操作篇
- clearerr
- fclose
- fdopen
- feof
- fflush
- fgetc
- fgets
- fileno
- fopen
- fputc
- fputs
- fread
- freopen
- fseek
- ftell
- fwrite
- getc
- getchar
- gets
- mktemp
- putc
- putchar
- rewind
- setbuf
- setbuffer
- setlinebuf
- setvbuf
- ungetc
- 進程操作篇
- atexit
- execl
- execlp
- execv
- execve
- execvp
- exit
- _exit
- vfork
- getpgid
- getpgrp
- getpid
- getppid
- getpriority
- nice
- on_exit
- setpgid
- setpgrp
- setpriority
- system
- wait
- waitpid
- 格式化輸入輸出篇
- fprintf
- fscanf
- printf
- scanf
- sprintf
- sscanf
- vfprintf
- vfscanf
- vprintf
- vscanf
- vsprintf
- vsscanf
- 文件權限控制篇
- access
- alphasort
- chdir
- chmod
- chown
- chroot
- closedir
- fchdir
- fchmod
- fchown
- fstat
- ftruncate
- getcwd
- link
- lstat
- opendir
- readdir
- readlink
- remove
- rename
- rewinddir
- seekdir
- stat
- symlink
- telldir
- truncate
- umask
- unlink
- utime
- utimes
- 信號處理篇
- alarm
- kill
- pause
- sigaction
- sigaddset
- sigdelset
- sigemptyset
- sigfillset
- sigismember
- signal
- sigpending
- sigprocmask
- sleep
- ferror
- perror
- strerror
- mkfifo
- pclose
- pipe
- popen
- 接口處理篇
- accept
- bind
- connect
- endprotoent
- endservent
- getsockopt
- htonl
- htons
- inet_addr
- inet_aton
- inet_ntoa
- listen
- ntohl
- ntohs
- recv
- recvfrom
- recvmsg
- send
- sendmsg
- sendto
- setprotoent
- setservent
- setsockopt
- shutdown
- socket
- 環境變量篇
- getenv
- putenv
- setenv
- 終端控制篇
- getopt
- isatty
- select
- ttyname