### Function

# GLib Array binary_search

since: 2.62

#### Declaration [src]

    gboolean
    g_array_binary_search (
      GArray* array,
      gconstpointer target,
      GCompareFunc compare_func,
      guint* out_match_index
    )

#### Description [src]

Checks whether `target` exists in `array` by performing a binary search based on the given comparison function `compare_func` which gets pointers to items as arguments. If the element is found, true is returned and the element’s index is returned in `out_match_index` (if non-`NULL`). Otherwise, false is returned and `out_match_index` is undefined. This search is using a binary search, so the `array` must absolutely be sorted to return a correct result (if not, the function may produce false-negative).

This example defines a comparison function and searches an element in a `GArray`:

    static gint
    cmpint (gconstpointer a, gconstpointer b)
    {
      const gint *_a = a;
      const gint *_b = b;

      return *_a - *_b;
    }
    ...
    gint i = 424242;
    guint matched_index;
    gboolean result = g_array_binary_search (garray, &i, cmpint, &matched_index);
    ...

Available since: 2.62

This function is not directly available to language bindings.

#### Parameters

`array`

  Type: An array of `gpointer`

  An array.

  The data is owned by the caller of the function.

`target`

  Type: `gconstpointer`

  A pointer to the item to look up.

  The argument can be `NULL`.
  The data is owned by the caller of the function.

`compare_func`

  Type: `GCompareFunc`

  A comparison function to locate `target`.

`out_match_index`

  Type: `guint*`

  The return location for the index of the element, if found.

  The argument will be set by the function.
  The argument can be `NULL`.

#### Return value

Type: `gboolean`

True if `target` is one of the elements of `array`; false otherwise.