# BSEARCHQQ

Portability Function: Performs a binary search of a sorted one-dimensional array for a specified element. The array elements cannot be derived types or structures.

## Module

USE IFPORT

result = BSEARCHQQ (adrkey,adrarray,length,size)

adrkey | (Input) INTEGER(4) on IA-32 architecture; INTEGER(8) on Intel® 64 architecture. Address of the variable containing the element to be found (returned by LOC).
adrarray | (Input) INTEGER(4) on IA-32 architecture; INTEGER(8) on Intel® 64 architecture. Address of the array (returned by LOC).
length | (Input) INTEGER(4) on IA-32 architecture; INTEGER(8) on Intel® 64 architecture. Number of elements in the array.
size | (Input) INTEGER(4). Positive constant less than 32,767 that specifies the kind of array to be sorted. The following constants, defined in `IFPORT.F90`, specify type and kind for numeric arrays:
Constant | Type of array
--- | ---
SRT$INTEGER1 | INTEGER(1)
SRT$INTEGER2 | INTEGER(2) or equivalent
SRT$INTEGER4 | INTEGER(4) or equivalent
SRT$INTEGER8 | INTEGER(8) or equivalent
SRT$REAL4 | REAL(4) or equivalent
SRT$REAL8 | REAL(8) or equivalent
SRT$REAL16 | REAL(16) or equivalent

If the value provided in size is not a symbolic constant and is less than 32,767, the array is assumed to be a character array with size characters per element.

## Results

The result type is INTEGER(4). It is an array index of the matched entry, or 0 if the entry is not found.

The array must be sorted in ascending order before being searched.

### CAUTION

The location of the array and the element to be found must both be passed by address using the LOC function. This defeats Fortran type checking, so you must make certain that the length and size arguments are correct, and that size is the same for the element to be found and the array searched.

If you pass invalid arguments, BSEARCHQQ attempts to search random parts of memory. If the memory it attempts to search is not allocated to the current process, the program is halted, and you receive a General Protection Violation message.

## Example

    USE IFPORT
    INTEGER(4) array(10), length
    INTEGER(4) result, target
    length = SIZE(array)
    ...
    result = BSEARCHQQ(LOC(target),LOC(array),length,SRT$INTEGER4)

Parent topic: A to B

## See Also

SORTQQ
LOC