.\" -*- 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 Noviembre 2020" "" "Manual del Programador de Linux" .SH NOMBRE bsearch \- búsqueda binaria en un arreglo (array) ordenado .SH SINOPSIS .nf \fB#include \fP .PP \fBvoid *bsearch(const void *\fP\fIkey\fP\fB, const void *\fP\fIbase\fP\fB,\fP \fB size_t \fP\fInmemb\fP\fB, size_t \fP\fIsize\fP\fB,\fP \fB int (*\fP\fIcompar\fP\fB)(const void *, const void *));\fP .fi .SH DESCRIPCIÓN La función \fBbsearch\fP() busca en un arreglo de \fInmemb\fP elementos, donde el primer elemento está apuntado por \fIbase\fP, un elemento que coincida con el objecto apuntado por \fIkey\fP. El tamaño de cada elementos del arreglo es especificado por \fIsize\fP. .PP El contenido del arreglo debe estar ordenado en orden ascendente según la función de comparación referenciada por \fIcompar\fP. Se espera que la rutina \fIcompar\fP tenga dos argumentos que apunten al objeto \fIkey\fP y a un elemento del arreglo, en ese orden, y debe devolver un entero menor, igual o mayor que cero si resulta que el objeto \fIkey\fP es menor, igual o mayor (respectivamente) que el elemento del arreglo. .SH "VALOR DEVUELTO" La función \fBbsearch\fP() devuelve un puntero al elemento del arreglo que coincide, o NULL si no hay coincidencia. Si hay múltiples elementos que coinciden con la clave, el elemento devuelto está sin determinar. .SH ATRIBUTOS Para obtener una explicación de los términos usados en esta sección, véase \fBattributes\fP(7). .TS allbox; lb lb lb l l l. Interfaz Atributo Valor T{ \fBbsearch\fP() T} Seguridad del hilo Multi\-hilo seguro .TE .sp 1 .SH "CONFORME A" POSIX.1\-2001, POSIX.1\-2008, C89, C99, SVr4, 4.3BSD. .SH EJEMPLOS The example below first sorts an array of structures using \fBqsort\fP(3), then retrieves desired elements using \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 "VÉASE TAMBIÉN" \fBhsearch\fP(3), \fBlsearch\fP(3), \fBqsort\fP(3), \fBtsearch\fP(3) .SH COLOFÓN Esta página es parte de la versión 5.10 del proyecto Linux \fIman\-pages\fP. Puede encontrar una descripción del proyecto, información sobre cómo informar errores y la última versión de esta página en \%https://www.kernel.org/doc/man\-pages/. .PP .SH TRADUCCIÓN La traducción al español de esta página del manual fue creada por Sebastian Desimone y Juan Pablo Puerta . .PP Esta traducción es documentación libre; lea la .UR https://www.gnu.org/licenses/gpl-3.0.html GNU General Public License Version 3 .UE o posterior con respecto a las condiciones de copyright. No existe NINGUNA RESPONSABILIDAD. .PP Si encuentra algún error en la traducción de esta página del manual, envíe un correo electrónico a .MT debian-l10n-spanish@lists.debian.org .ME .