Reader small image

You're reading from  Java Coding Problems - Second Edition

Product typeBook
Published inMar 2024
PublisherPackt
ISBN-139781837633944
Edition2nd Edition
Right arrow
Author (1)
Anghel Leonard
Anghel Leonard
author image
Anghel Leonard

Anghel Leonard is a Chief Technology Strategist and independent consultant with 20+ years of experience in the Java ecosystem. In daily work, he is focused on architecting and developing Java distributed applications that empower robust architectures, clean code, and high-performance. Also passionate about coaching, mentoring and technical leadership. He is the author of several books, videos and dozens of articles related to Java technologies.
Read more about Anghel Leonard

Right arrow

169. Calling the bsearch() foreign function

The bsearch() foreign function is part of the C standard library and has the following signature (https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/bsearch):

void *bsearch(
  const void *key,
  const void *base,
  size_t num,
  size_t width,
  int ( __cdecl *compare ) (
    const void *key, const void *datum)
);

In a nutshell, this method gets pointers to a key, a sorted array (base), and a comparator. Its goal is to use the given comparator to perform a binary search of the given key in the given array. More precisely, bsearch() gets a pointer to the key, a pointer to the array, the number of elements in the array (num), the size of an element in bytes (width), and the comparator as a callback function.

The callback function gets a pointer to key and a pointer to the current element of the array to be compared with key. It returns the result of comparing these two elements.

The bsearch() function returns...

lock icon
The rest of the page is locked
Previous PageNext Page
You have been reading a chapter from
Java Coding Problems - Second Edition
Published in: Mar 2024Publisher: PacktISBN-13: 9781837633944

Author (1)

author image
Anghel Leonard

Anghel Leonard is a Chief Technology Strategist and independent consultant with 20+ years of experience in the Java ecosystem. In daily work, he is focused on architecting and developing Java distributed applications that empower robust architectures, clean code, and high-performance. Also passionate about coaching, mentoring and technical leadership. He is the author of several books, videos and dozens of articles related to Java technologies.
Read more about Anghel Leonard