BSEARCH(3POSIX)
π BSEARCH(3POSIX) π
okay bestie, welcome to the POSIX Programmer's Manual, page BSEARCH(3POSIX). this is the whole vibe.
π«Ά PROLOG
so like, real talk first. this manual page is part of the POSIX Programmer's Manual. the Linux implementation of this interface may differ (so go consult the corresponding Linux manual page for details on Linux behavior, fr), or honestly the interface may not be implemented on Linux at all. not every baddie ships everywhere π
β¨ NAME β¨
bsearch β binary search a sorted table. she searches. she finds. that's the whole personality π
πΈ SYNOPSIS πΈ
#include <stdlib.h>
void *bsearch(const void *key, const void *base, size_t nel,
size_t width, int (*compar)(const void *, const void *));
π¦ DESCRIPTION π¦
okay so the functionality described on this reference page is aligned with the ISO C standard. any conflict between the requirements described here and the ISO C standard is unintentional β no drama, it's an accident. this volume of POSIX.1β2017 defers to the ISO C standard. periodt.
The bsearch() function shall search an array of nel objects, the initial element of which is pointed to by base, for an element that matches the object pointed to by key. the size of each element in the array is specified by width. and here's the cute edge case: if the nel argument has the value zero, the comparison function pointed to by compar shall not be called and no match shall be found. like, nothing happens, bestie, it's a no-op π
The comparison function pointed to by compar shall be called with two arguments that point to the key object and to an array element, in that order. key first, element second. order matters, iykyk π
The application shall ensure that the comparison function pointed to by compar does not alter the contents of the array. the implementation may reorder elements of the array between calls to the comparison function, but shall not alter the contents of any individual element. so she can shuffle the deck but she can't scribble on the cards π
The implementation shall ensure that the first argument is always a pointer to the key. always. every time. iconic consistency.
When the same objects (consisting of width bytes, irrespective of their current positions in the array) are passed more than once to the comparison function, the results shall be consistent with one another. that is, the same object shall always compare the same way with the key. no flip-flopping, we don't do that here π
The application shall ensure that the function returns an integer less than, equal to, or greater than 0 if the key object is considered, respectively, to be less than, to match, or to be greater than the array element. and the application shall ensure that the array consists of all the elements that compare less than, all the elements that compare equal to, and all the elements that compare greater than the key object, in that order. sorted. literally the whole point. if your array isn't sorted like that, this function is not going to have a good time and neither are you π
π RETURN VALUE π
The bsearch() function shall return a pointer to a matching member of the array, or a null pointer if no match is found. and if two or more members compare equal? which member is returned is unspecified. so don't get attached to a specific one, she picks whichever, it's giving mystery β¨
π ERRORS π
No errors are defined. zero. none. a clean page, we love to see it.
The following sections are informative.
π₯ EXAMPLES π₯
The example below searches a table containing pointers to nodes consisting of a string and its length. the table is ordered alphabetically on the string in the node pointed to by each entry.
The code fragment below reads in strings and either finds the corresponding node and prints out the string and its length, or prints an error message.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define TABSIZE 1000
struct node { /* These are stored in the table. */
char *string;
int length;
};
struct node table[TABSIZE]; /* Table to be searched. */
.
.
.
{
struct node *node_ptr, node;
/* Routine to compare 2 nodes. */
int node_compare(const void *, const void *);
.
.
.
while (scanf("%ms", &node.string) != EOF) {
node_ptr = (struct node *)bsearch((void *)(&node),
(void *)table, TABSIZE,
sizeof(struct node), node_compare);
if (node_ptr != NULL) {
(void)printf("string = %20s, length = %d\n",
node_ptr->string, node_ptr->length);
} else {
(void)printf("not found: %s\n", node.string);
}
free(node.string);
}
}
/*
This routine compares two nodes based on an
alphabetical ordering of the string field.
*/
int
node_compare(const void *node1, const void *node2)
{
return strcoll(((const struct node *)node1)->string,
((const struct node *)node2)->string);
}
π APPLICATION USAGE π
The pointers to the key and the element at the base of the table should be of type pointer-to-element.
The comparison function need not compare every byte, so arbitrary data may be contained in the elements in addition to the values being compared. extra bytes just vibing in there, and that's fine π§
In practice, the array is usually sorted according to the comparison function. like, obviously.
π RATIONALE π
The requirement that the second argument (hereafter referred to as p) to the comparison function is a pointer to an element of the array implies that for every call all of the following expressions are non-zero:
( (char *)p - (char *)base ) % width == 0
(char *)p >= (char *)base
(char *)p < (char *)base + nel * width
math check but make it mandatory. she's aligned, she's in bounds, she's exactly one element wide. no cap π
β FUTURE DIRECTIONS β
None. we're done here. nothing coming. the future is quiet, bestie.
π SEE ALSO π
hcreate()lsearch()qsort()tdelete()
The Base Definitions volume of POSIX.1β2017, <stdlib.h>
π COPYRIGHT π
Portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1-2017, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. In the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained online at http://www.opengroup.org/unix/online.html .
Any typographical or formatting errors that appear in this page are most likely to have been introduced during the conversion of the source files to man page format. To report such errors, see https://www.kernel.org/doc/man-pages/reporting_bugs.html .
and that's the whole page, bestie π«Ά shoutout to IEEE/The Open Group, 2017, for blessing us with this one. BSEARCH(3POSIX) out. slay π
β¨
π¦ where this page comes from
- debian bookworm: manpages-posix-dev 2017a-2 Β· Other
- debian trixie: manpages-posix-dev 2017a-2 Β· Other
- ubuntu jammy: manpages-posix-dev 2017a-2 Β· Other
- ubuntu noble: manpages-posix-dev 2017a-2 Β· Other
- ubuntu resolute: manpages-posix-dev 2017a-2build1 Β· Other
- ubuntu stonking: manpages-posix-dev 2017a-2.1 Β· Other
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms