tfind() — Binary tree find node

Standards

Standards / Extensions C or C++ Dependencies

XPG4
XPG4.2
Single UNIX Specification, Version 3

both  

Format

#define _XOPEN_SOURCE
#include <search.h>

void *tfind(const void *key, void *const *rootp,
            int (*compar)(const void *, const void *));

General description

The tfind() function, like tsearch(), will search for a node in the tree, returning a pointer to it if found. However, if it is not found, the tfind() function will return a NULL pointer. The arguments for the tfind() function are the same as for the tsearch() function.

Comparisons are made with a user-supplied routine, the address of which is passed as the compar argument. This routine is called with two arguments, the pointers to the elements being compared. The user-supplied routine must return an integer less than, equal to or greater than 0, according to whether the first argument is to be considered less than, equal to or greater than the second argument. The comparison functions need not compare every byte, so arbitrary data may be contained in the elements in addition to the values being compared.

Threading Behavior: see tsearch() — Binary tree search.

Special behavior for C++: Because C and C++ linkage conventions are incompatible, tfind() cannot receive a C++ function pointer as the comparator argument. If you attempt to pass a C++ function pointer to tfind(), the compiler will flag it as an error. You can pass a C or C++ function to tfind() by declaring it as extern "C".

Returned value

If the node is found, tfind() returns a pointer to it.

If unsuccessful, tfind() returns a NULL pointer.

If rootp is a NULL pointer on entry, tfind() returns a NULL pointer.

No errors are defined.

Related information