📄 lsearch.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"><html><head><!-- Copyright 1997 The Open Group, All Rights Reserved --><title>lsearch</title></head><body bgcolor=white><center><font size=2>The Single UNIX ® Specification, Version 2<br>Copyright © 1997 The Open Group</font></center><hr size=2 noshade><h4><a name = "tag_000_007_460"> </a>NAME</h4><blockquote>lsearch, lfind - linear search and update</blockquote><h4><a name = "tag_000_007_461"> </a>SYNOPSIS</h4><blockquote><pre><code>#include <<a href="search.h.html">search.h</a>>void *lsearch(const void *<i>key</i>, void *<i>base</i>, size_t *<i>nelp</i>, size_t <i>width</i>, int (*<i>compar</i>)(const void *, const void *));void *lfind(const void *<i>key</i>, const void *<i>base</i>, size_t *<i>nelp</i>, size_t width, int (*<i>compar</i>)(const void *, const void *));</code></pre></blockquote><h4><a name = "tag_000_007_462"> </a>DESCRIPTION</h4><blockquote>The<i>lsearch()</i>function is a linear search routine. It returns a pointer into a tableindicating where an entry may be found. If the entry does not occur, it isadded at the end of the table. The<i>key</i>argument points to the entry to be sought in the table. The<i>base</i>argument points to the first element in the table. The<i>width</i>argument is the size of an element in bytes. The<i>nelp</i>argument points to an integer containing the current number of elements in thetable. The integer to which<i>nelp</i>points is incremented if the entry is added to the table. The<i>compar</i>argument points to a comparison function which the user must supply (.Fn strcmp ,for example). It is called with two arguments that point to the elementsbeing compared. The function must return 0 if the elements are equal andnon-zero otherwise.<p>The<i><a href="lfind.html">lfind()</a></i>function is the same as<i>lsearch()</i>except that if the entry is not found, it is not added to the table. Instead,a null pointer is returned.</blockquote><h4><a name = "tag_000_007_463"> </a>RETURN VALUE</h4><blockquote>If the searched for entry is found, both<i>lsearch()</i>and<i><a href="lfind.html">lfind()</a></i>return a pointer to it. Otherwise,<i><a href="lfind.html">lfind()</a></i>returns a null pointer and<i>lsearch()</i>returns a pointer to the newly added element.<p>Both functions return a null pointer in case of error.</blockquote><h4><a name = "tag_000_007_464"> </a>ERRORS</h4><blockquote>No errors are defined.<br></blockquote><h4><a name = "tag_000_007_465"> </a>EXAMPLES</h4><blockquote>This fragment will read in less than or equal to TABSIZEstrings of length less than or equal to ELSIZEand store them in a table, eliminating duplicates.<pre><code>#include <stdio.h>#include <string.h>#include <search.h>#define TABSIZE 50#define ELSIZE 120 ... char line[ELSIZE], tab[TABSIZE][ELSIZE]; size_t nel = 0; ... while (fgets(line, ELSIZE, stdin) != NULL && nel < TABSIZE) (void) lsearch(line, tab, &nel, ELSIZE, (int (*)(const void *, const void *)) strcmp); ...</code></pre></blockquote><h4><a name = "tag_000_007_466"> </a>APPLICATION USAGE</h4><blockquote>The comparison function need not compare every byte, so arbitrarydata may be contained in the elements in addition to the valuesbeing compared.<p>Undefined results can occur if there is not enough room in the table toadd a new item.</blockquote><h4><a name = "tag_000_007_467"> </a>FUTURE DIRECTIONS</h4><blockquote>None.</blockquote><h4><a name = "tag_000_007_468"> </a>SEE ALSO</h4><blockquote><i><a href="bsearch.html">bsearch()</a></i>,<i><a href="hsearch.html">hsearch()</a></i>,<i><a href="tsearch.html">tsearch()</a></i>,<i><a href="search.h.html"><search.h></a></i>.</blockquote><h4>DERIVATION</h4><blockquote>Derived from Issue 1 of the SVID.</blockquote><hr size=2 noshade><center><font size=2>UNIX ® is a registered Trademark of The Open Group.<br>Copyright © 1997 The Open Group<br> [ <a href="../index.html">Main Index</a> | <a href="../xshix.html">XSH</a> | <a href="../xcuix.html">XCU</a> | <a href="../xbdix.html">XBD</a> | <a href="../cursesix.html">XCURSES</a> | <a href="../xnsix.html">XNS</a> ]</font></center><hr size=2 noshade></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -