bsearch is a function that performs a binary search. This function may exist in various programming languages, such as C. Use this tag ONLY for existing library functions named "bsearch". Use the [binary-search] tag instead for questions about binary search or self-written implementations.
Questions tagged [bsearch]
87 questions
1
vote
3 answers
How is the bsearch() function in the standard C library implemented?
Does anyone know how the standard binary search function is implemented?
This is the prototype.
void * bsearch (const void*, const void*, size_t, size_t, int (*) (const void *, const void *) );
I'm really curious about how they used void pointers.

Davide Valdo
- 779
- 8
- 21
1
vote
1 answer
bsearch changing key in loop
Is there anyway to do something like this?
int key=50;
int loop=5;
int array[10]={...};
int* Ptr=NULL;
qsort(array, 10, sizeof(int), compareints);
while(loop>0){
Ptr=(int*)bsearch(&key,array,10,sizeof(int),compareints);
if(Ptr!=NULL){
…

H_squared
- 1,251
- 2
- 15
- 32
1
vote
2 answers
Why does the call to bsearch() crash the presented program?
I have an unsorted dictionary file named "dict.txt".
I have managed to put the words of the file in an array and the qsort() I use also seems to be working just fine (That is, the array is sorted).
The problem arises when I call bsearch(),
the…

dankilman
- 886
- 2
- 9
- 18
1
vote
1 answer
Determining index from bsearch and lfind?
I'm trying to get the index of the element in the array after lfind and bsearch return the pointer to the element that it found. I have this so far:
(char *) (found - cv->baseAddress);
where found is the address of what the functions found, and…

girlrockingguna
- 291
- 1
- 3
- 13
1
vote
0 answers
Bsearch in C to match within a range of elements, instead of a specific element
I'm trying to do a binary search of an array of structs which contain hex addresses. Each struct holds a few addresses, and when I give an input address, I want to find the struct that has the range which spans my input address, not just the exact…

user2057841
- 213
- 1
- 4
- 14
1
vote
1 answer
Seeking a GMP binary search: How to compare two GMP mpz_t's using memcmp?
Motivation: I want to use bsearch (binary search) to quickly search through a sorted list of 121-bit non-negative integers (they all have exactly 121 bits, although they may have leading zeroes). These integers are too large to stored as…

Douglas S. Stones
- 541
- 1
- 4
- 14
1
vote
3 answers
pointer from integer w/o cast warning when calling lfind
I'm writing a vector in C. The CVectorSearch function uses bsearch if it's sorted, and lfind if it's unsorted. Why am I getting the warning "assignment makes pointer from integer without a cast" when I'm calling lfind? It seems to work fine even…
Thomas
1
vote
1 answer
bsearch with dynamic arrays in C
Hello I have some problems with the bsearch() function. I get the "Access violation reading location" exception.
I'm using it as follows:
typedef char **arstr;
int compareexp(const void *a, const void *b){
return strcmp(*(const arstr)a,…

Frozen_byte
- 272
- 4
- 12
0
votes
1 answer
bsearch on multiple items in struct
Fixed.
include
main()
{
int n;
int i;
char tempMonth[255]; //Used to store the month until checked
scanf("%d", &n);
struct date *list;
list = (struct date *)malloc((n * sizeof(struct date)));
for(i = 0; i < n; i++)
…

Mike
- 109
- 2
- 10
0
votes
1 answer
Find person with surname that starts with character c using bsearch
I have problem finding what is wrong with my code down below. It compiles, but doesn't work properly. What it has to do is to find person with surname that starts with character c using bsearch.
#include
#include
…

smth
- 103
- 1
0
votes
1 answer
Search for a target string using bsearch in an array of string in a struct
Hi I'm using bsearch to search for a target string in an array of string in a struct and return the index of the string. However, so far it is always returning NULL, and I'm pretty sure the way I'm getting the array index is also not right:
#include…

DavidKanes
- 183
- 1
- 1
- 10
0
votes
1 answer
(C) Finding struct by name in struct array with bsearch
I'm trying to implement function (1) for finding a specific Product by name in struct array. p_array is the struct array containing n number of Products, search_key is pointer to a string with the name of the searched structure cmp is pointer to…

mushroom_man
- 1
- 1
0
votes
2 answers
bsearch with an array of structs
I have an array of structs that has already been sorted using qsort. I'm trying to search for a name in struct however it is always returning NULL. What is the explanation for why this is happening?
Here is my code:
#include "card.h"
#include…

csDreamer
- 11
- 3
0
votes
1 answer
The compar callback function for bsearch needs only implementation of equality?
From cppreference,
To perform the search, the function performs a series of calls to
compar with key as first argument and elements of the array pointed to
by base as second argument.
For a given array int arr[] = {1, 2, 3, 4, 5, 6, 7}, searching…

Cătălina Sîrbu
- 1,253
- 9
- 30
0
votes
3 answers
Can an array be ordered both ascendant and descendant for bsearch to work?
From this link
Because this function may be optimized to use a non-linear search
algorithm (presumably a binary search), the elements that compare less
than key using compar should precede those that compare equal, and
these should precede those…

Cătălina Sîrbu
- 1,253
- 9
- 30