SDL_bsearch(3)
β¨ NAME β¨
SDL_bsearch β bestie, this is the glow-up version of a binary search: you hand it an array you already sorted, and it finds your element in it. π
This one lives in the SDL3 FUNCTIONS family, so you already know it's giving main character energy.
πΈ SYNOPSIS πΈ
#include <SDL3/SDL_stdinc.h>
void * SDL_bsearch(const void *key, const void *base, size_t nmemb, size_t size, SDL_CompareCallback compare);
π DESCRIPTION π
okay so the vibe here is: binary search, but make it SDL. You give it a key, an array that has already been sorted, and a compare callback, and it goes hunting. π
For example:
typedef struct {
int key;
const char *string;
} data;
int SDLCALL compare(const void *a, const void *b)
{
const data *A = (const data *)a;
const data *B = (const data *)b;
if (A->n < B->n) {
return -1;
} else if (B->n < A->n) {
return 1;
} else {
return 0;
}
}
data values[] = {
{ 1, "first" }, { 2, "second" }, { 3, "third" }
};
data key = { 2, NULL };
data *result = SDL_bsearch(&key, values, SDL_arraysize(values), sizeof(values[0]), compare);
literally just: key in, sorted array in, compare function in, result out. periodt. β¨
π¦ FUNCTION PARAMETERS π¦
keyβ a pointer to a key that's equal to the element you're searching for. like, this is the thing you want a twin of. π«Άbaseβ a pointer to the start of the array. the beginning, the origin story, the front row. πnmembβ the number of elements in the array. how many besties are even in the room. πsizeβ the size of the elements in the array. (per element, not the whole thing β iykyk.) βcompareβ a function used to compare elements in the array. you write it, SDL calls it, they gossip about ordering. π
π RETURN VALUE π
Returns a pointer to the matching element in the array, or NULL if not found.
so likeβ¦ pointer = found, NULL = it's not here bestie, go home. π
π§ THREAD SAFETY π§
It is safe to call this function from any thread. no cap. every thread can have a turn, no drama, no fighting. π
π AVAILABILITY π
This function is available since SDL 3.2.0. so if you're on something older than thatβ¦ it's giving vintage, but not in the cute way. π
π SEE ALSO π
lowkey reminder bestie: it's a binary search, so the array has to be sorted first. SDL_qsort is right there in SEE ALSO for a reason. sort then search, in that order. π
and that's the whole page, mwah π
Brought to you by the fine folks at Simple Directmedia Layer β this is from SDL 3.4.18, and the page itself is SDL_bsearch(3). go sort something and then find it, you icon. πβ¨π¦
π¦ where this page comes from
- fedora 43: SDL3-devel 3.4.18-1.fc43 Β· Zlib AND MIT AND Apache-2.0 AND (Apache-2.0 OR MIT)
- fedora 43: SDL3-devel 3.4.18-1.fc43 Β· Zlib AND MIT AND Apache-2.0 AND (Apache-2.0 OR MIT)
- fedora 44: SDL3-devel 3.4.18-1.fc44 Β· Zlib AND MIT AND Apache-2.0 AND (Apache-2.0 OR MIT)
- fedora 44: SDL3-devel 3.4.18-1.fc44 Β· Zlib AND MIT AND Apache-2.0 AND (Apache-2.0 OR MIT)
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms