{"id":7130,"date":"2022-12-20T19:35:34","date_gmt":"2022-12-20T22:35:34","guid":{"rendered":"http:\/\/lode.uno\/linux-man\/index.php\/2022\/12\/20\/bsearch-man3\/"},"modified":"2022-12-20T19:35:34","modified_gmt":"2022-12-20T22:35:34","slug":"bsearch-man3","status":"publish","type":"post","link":"https:\/\/lode.uno\/linux-man\/2022\/12\/20\/bsearch-man3\/","title":{"rendered":"BSEARCH (man3)"},"content":{"rendered":"<h1 align=\"center\">BSEARCH<\/h1>\n<p> <a href=\"#NAME\">NAME<\/a><br \/> <a href=\"#SYNOPSIS\">SYNOPSIS<\/a><br \/> <a href=\"#DESCRIPTION\">DESCRIPTION<\/a><br \/> <a href=\"#RETURN VALUE\">RETURN VALUE<\/a><br \/> <a href=\"#ATTRIBUTES\">ATTRIBUTES<\/a><br \/> <a href=\"#CONFORMING TO\">CONFORMING TO<\/a><br \/> <a href=\"#EXAMPLES\">EXAMPLES<\/a><br \/> <a href=\"#SEE ALSO\">SEE ALSO<\/a><br \/> <a href=\"#COLOPHON\">COLOPHON<\/a> <\/p>\n<hr>\n<h2>NAME <a name=\"NAME\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">bsearch \u2212 binary search of a sorted array<\/p>\n<h2>SYNOPSIS <a name=\"SYNOPSIS\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>#include <stdlib.h><\/b><\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>void *bsearch(const void *<\/b><i>key<\/i><b>, const void *<\/b><i>base<\/i><b>, <br \/> size_t<\/b> <i>nmemb<\/i><b>, size_t<\/b> <i>size<\/i><b>, <br \/> int (*<\/b><i>compar<\/i><b>)(const void *, const void *));<\/b><\/p>\n<h2>DESCRIPTION <a name=\"DESCRIPTION\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">The <b>bsearch<\/b>() function searches an array of <i>nmemb<\/i> objects, the initial member of which is pointed to by <i>base<\/i>, for a member that matches the object pointed to by <i>key<\/i>. The size of each member of the array is specified by <i>size<\/i>.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The contents of the array should be in ascending sorted order according to the comparison function referenced by <i>compar<\/i>. The <i>compar<\/i> routine is expected to have two arguments which point to the <i>key<\/i> object and to an array member, in that order, and should return an integer less than, equal to, or greater than zero if the <i>key<\/i> object is found, respectively, to be less than, to match, or be greater than the array member.<\/p>\n<h2>RETURN VALUE <a name=\"RETURN VALUE\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">The <b>bsearch<\/b>() function returns a pointer to a matching member of the array, or NULL if no match is found. If there are multiple elements that match the key, the element returned is unspecified.<\/p>\n<h2>ATTRIBUTES <a name=\"ATTRIBUTES\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">For an explanation of the terms used in this section, see <b>attributes<\/b>(7).<\/p>\n<table width=\"100%\" border=\"0\" rules=\"none\" frame=\"void\" cellspacing=\"0\" cellpadding=\"0\">\n<tr valign=\"top\" align=\"left\">\n<td width=\"8%\"><\/td>\n<td width=\"7%\"> <\/td>\n<td width=\"8%\"> <\/td>\n<td width=\"8%\"><\/td>\n<td width=\"7%\"><\/td>\n<td width=\"62%\"> <\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"8%\"><\/td>\n<td width=\"7%\"><\/td>\n<td width=\"8%\"> <\/td>\n<td width=\"8%\"><\/td>\n<td width=\"7%\"><\/td>\n<td width=\"62%\"> <\/td>\n<\/tr>\n<\/table>\n<p align=\"center\"><img decoding=\"async\" src=\"grohtml-1581221.png\" alt=\"Image grohtml-1581221.png\"><\/p>\n<h2>CONFORMING TO <a name=\"CONFORMING TO\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">POSIX.1-2001, POSIX.1-2008, C89, C99, SVr4, 4.3BSD.<\/p>\n<h2>EXAMPLES <a name=\"EXAMPLES\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">The example below first sorts an array of structures using <b>qsort<\/b>(3), then retrieves desired elements using <b>bsearch<\/b>().<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">#include <stdio.h> <br \/> #include <stdlib.h> <br \/> #include <string.h><\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">struct mi { <br \/> int nr; <br \/> char *name; <br \/> } months[] = { <br \/> { 1, &#8220;jan&#8221; }, { 2, &#8220;feb&#8221; }, { 3, &#8220;mar&#8221; }, { 4, &#8220;apr&#8221; }, <br \/> { 5, &#8220;may&#8221; }, { 6, &#8220;jun&#8221; }, { 7, &#8220;jul&#8221; }, { 8, &#8220;aug&#8221; }, <br \/> { 9, &#8220;sep&#8221; }, {10, &#8220;oct&#8221; }, {11, &#8220;nov&#8221; }, {12, &#8220;dec&#8221; } <br \/> };<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">#define nr_of_months (sizeof(months)\/sizeof(months[0]))<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">static int <br \/> compmi(const void *m1, const void *m2) <br \/> { <br \/> const struct mi *mi1 = m1; <br \/> const struct mi *mi2 = m2; <br \/> return strcmp(mi1\u2212>name, mi2\u2212>name); <br \/> }<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">int <br \/> main(int argc, char **argv) <br \/> { <br \/> qsort(months, nr_of_months, sizeof(months[0]), compmi); <br \/> for (int i = 1; i < argc; i++) { <br \/> struct mi key; <br \/> struct mi *res;<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">key.name = argv[i]; <br \/> res = bsearch(&#038;key, months, nr_of_months, <br \/> sizeof(months[0]), compmi); <br \/> if (res == NULL) <br \/> printf(&#8220;&#8216;%s&#8217;: unknown monthn&#8221;, argv[i]); <br \/> else <br \/> printf(&#8220;%s: month #%dn&#8221;, res\u2212>name, res\u2212>nr); <br \/> } <br \/> exit(EXIT_SUCCESS); <br \/> }<\/p>\n<h2>SEE ALSO <a name=\"SEE ALSO\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>hsearch<\/b>(3), <b>lsearch<\/b>(3), <b>qsort<\/b>(3), <b>tsearch<\/b>(3)<\/p>\n<h2>COLOPHON <a name=\"COLOPHON\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">This page is part of release 5.10 of the Linux <i>man-pages<\/i> project. A description of the project, information about reporting bugs, and the latest version of this page, can be found at https:\/\/www.kernel.org\/doc\/man\u2212pages\/.<\/p>\n<hr>\n","protected":false},"excerpt":{"rendered":"<p>  bsearch \u2212 binary search of a sorted array <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2536],"tags":[2538,1885,3007],"class_list":["post-7130","post","type-post","status-publish","format-standard","hentry","category-3-llamadas-de-bibliotecas","tag-2538","tag-bsearch","tag-man3"],"gutentor_comment":0,"_links":{"self":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts\/7130","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/comments?post=7130"}],"version-history":[{"count":0,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts\/7130\/revisions"}],"wp:attachment":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/media?parent=7130"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/categories?post=7130"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/tags?post=7130"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}