.\" -*- coding: UTF-8 -*- .\" Copyright 1993 David Metcalfe (david@prism.demon.co.uk) .\" .\" %%%LICENSE_START(VERBATIM) .\" Permission is granted to make and distribute verbatim copies of this .\" manual provided the copyright notice and this permission notice are .\" preserved on all copies. .\" .\" Permission is granted to copy and distribute modified versions of this .\" manual under the conditions for verbatim copying, provided that the .\" entire resulting derived work is distributed under the terms of a .\" permission notice identical to this one. .\" .\" Since the Linux kernel and libraries are constantly changing, this .\" manual page may be incorrect or out-of-date. The author(s) assume no .\" responsibility for errors or omissions, or for damages resulting from .\" the use of the information contained herein. The author(s) may not .\" have taken the same level of care in the production of this manual, .\" which is licensed free of charge, as they might when working .\" professionally. .\" .\" Formatted or processed versions of this manual, if unaccompanied by .\" the source, must acknowledge the copyright and authors of this work. .\" %%%LICENSE_END .\" .\" References consulted: .\" Linux libc source code .\" Lewine's _POSIX Programmer's Guide_ (O'Reilly & Associates, 1991) .\" 386BSD man pages .\" Modified Mon Mar 29 22:41:16 1993, David Metcalfe .\" Modified Sat Jul 24 21:35:16 1993, Rik Faith (faith@cs.unc.edu) .\"******************************************************************* .\" .\" This file was generated with po4a. Translate the source file. .\" .\"******************************************************************* .TH BSEARCH 3 "1 novembro 2020" "" "Manual do Programador do Linux" .SH NOME bsearch \- busca binária em um array ordenado .SH SINOPSE .nf \fB#include \fP .PP \fBvoid *bsearch(const void *\fP\fIchave\fP\fB, const void *\fP\fIbase\fP\fB,\fP \fB size_t \fP\fInmemb\fP\fB, size_t \fP\fItam\fP\fB,\fP \fB int (*\fP\fIcompar\fP\fB)(const void *, const void *));\fP .fi .SH DESCRIÇÃO A função \fBbsearch\fP() busca em um array de \fInmemb\fP objetos, a partir do membro inicial apontado por \fIbase\fP, por um membro que corresponde ao objeto apontado por \fIchave\fP. O tamanho de cada membro do array é especificado por \fItam\fP. .PP O conteúdo do array deve estar em ordem crescente de acordo com a função de comparação referenciada por \fIcompar\fP. Espera\-se que a rotina \fIcompar\fP tenha dois argumentos que apontam para o objeto \fIchave\fP e para um membro do array, nesta ordem, e deve retornar um número inteiro menor, igual ou maior que zero se o objeto \fIchave\fP for descoberto, respectivamente, como sendo menor que, igual a ou maior que o membro do array. .SH "VALOR DE RETORNO" A função \fBbsearch\fP() retorna um ponteiro para um membro correspondente do array, ou NULO se nenhuma correspondência foi encontrada. Se houver vários elementos que correspondem à chave, o elemento retornado é não especificado. .SH ATRIBUTOS Para uma explicação dos termos usados nesta seção, consulte \fBattributes\fP(7). .TS allbox; lb lb lb l l l. Interface Atributo Valor T{ \fBbsearch\fP() T} Thread safety MT\-Safe .TE .sp 1 .SH "DE ACORDO COM" POSIX.1\-2001, POSIX.1\-2008, C89, C99, SVr4, 4.3BSD. .SH EXEMPLOS O exemplo abaixo primeiro classifica um array de estruturas usando \fBqsort\fP(3) e, em seguida, recupera os elementos desejados usando \fBbsearch\fP(). .PP .EX #include #include #include struct mi { int nr; char *name; } months[] = { { 1, "jan" }, { 2, "feb" }, { 3, "mar" }, { 4, "apr" }, { 5, "may" }, { 6, "jun" }, { 7, "jul" }, { 8, "aug" }, { 9, "sep" }, {10, "oct" }, {11, "nov" }, {12, "dec" } }; #define nr_of_months (sizeof(months)/sizeof(months[0])) static int compmi(const void *m1, const void *m2) { const struct mi *mi1 = m1; const struct mi *mi2 = m2; return strcmp(mi1\->name, mi2\->name); } int main(int argc, char **argv) { qsort(months, nr_of_months, sizeof(months[0]), compmi); for (int i = 1; i < argc; i++) { struct mi key; struct mi *res; key.name = argv[i]; res = bsearch(&key, months, nr_of_months, sizeof(months[0]), compmi); if (res == NULL) printf("\(aq%s\(aq: unknown month\en", argv[i]); else printf("%s: month #%d\en", res\->name, res\->nr); } exit(EXIT_SUCCESS); } .EE .\" this example referred to in qsort.3 .SH "VEJA TAMBÉM" \fBhsearch\fP(3), \fBlsearch\fP(3), \fBqsort\fP(3), \fBtsearch\fP(3) .SH COLOFÃO Esta página faz parte da versão 5.10 do projeto Linux \fIman\-pages\fP. Uma descrição do projeto, informações sobre relatórios de bugs e a versão mais recente desta página podem ser encontradas em \%https://www.kernel.org/doc/man\-pages/. .PP .SH TRADUÇÃO A tradução para português brasileiro desta página man foi criada por Paulo César Mendes , André Luiz Fassone e Rafael Fontenelle . . .PP Esta tradução é uma documentação livre; leia a .UR https://www.gnu.org/licenses/gpl-3.0.html Licença Pública Geral GNU Versão 3 .UE ou posterior para as condições de direitos autorais. Nenhuma responsabilidade é aceita. .PP Se você encontrar algum erro na tradução desta página de manual, envie um e-mail para .MT debian-l10n-portuguese@lists.debian.org a lista de discussão de tradutores .ME .