.\" -*- coding: UTF-8 -*- '\" t .\" Copyright 1993, David Metcalfe .\" Copyright, the authors of the Linux man-pages project .\" .\" SPDX-License-Identifier: Linux-man-pages-copyleft .\" .\"******************************************************************* .\" .\" This file was generated with po4a. Translate the source file. .\" .\"******************************************************************* .TH bsearch 3 "10 Febrero 2026" "Linux man\-pages 6.18" .SH NOMBRE bsearch \- búsqueda binaria en un arreglo (array) ordenado .SH BIBLIOTECA Biblioteca Estándar C (\fIlibc\fP,\ \fI\-lc\fP) .SH SINOPSIS .nf \fB#include \fP .P \fBvoid *bsearch(\fPsize_t n, size_t size; \fB const void \fP\fIkey\fP\fB[\fP\fIsize\fP\fB], const void \fP\fIbase\fP\fB[\fP\fIsize\fP\fB * \fP\fIn\fP\fB],\fP \fB size_t \fP\fIn\fP\fB, size_t \fP\fIsize\fP\fB,\fP \fB typeof(int (const void [\fP\fIsize\fP\fB], const void [\fP\fIsize\fP\fB]))\fP \fB *\fP\fIcompar\fP\fB);\fP .fi .SH DESCRIPCIÓN The \fBbsearch\fP() function searches an array of \fIn\fP objects, the initial member of which is pointed to by \fIbase\fP, for a member that matches the object pointed to by \fIkey\fP. The size of each member of the array is specified by \fIsize\fP. .P 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; lbx lb lb l l l. Interfaz Atributo Valor T{ .na .nh \fBbsearch\fP() T} Seguridad del hilo Multi\-hilo seguro .TE .SH ESTÁNDARES C11, POSIX.1\-2008. .SH HISTORIAL POSIX.1\-2001, 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(). .P .\" SRC BEGIN (bsearch.c) .EX #include #include #include #include \& struct mi { int nr; const char *name; }; \& static struct mi 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" } }; \& 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, countof(months), sizeof(months[0]), compmi); for (size_t i = 1; i < argc; i++) { struct mi key; struct mi *res; \& key.name = argv[i]; res = bsearch(&key, months, countof(months), sizeof(months[0]), compmi); if (res == NULL) printf("\[aq]%s\[aq]: unknown month\[rs]n", argv[i]); else printf("%s: month #%d\[rs]n", res\->name, res\->nr); } exit(EXIT_SUCCESS); } .EE .\" SRC END .SH "VÉASE TAMBIÉN" \fBhsearch\fP(3), \fBlsearch\fP(3), \fBqsort\fP(3), \fBtsearch\fP(3) .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 .