#define u32 uint32_t
#define Narray 10
struct edge {
u32 v1;
u32 v2;
};
int struct_cmp_by_v1(const void *i, const void *j) {
struct edge *a = (struct edge *)i;
struct edge *b = (struct edge *)j;
u32 x = a->v1;
u32 y = b->v1;
return x > y ? 1 : -1;
}
struct edge *array = malloc((sizeof(struct edge))*Narray);
struct edge *l = malloc(sizeof(struct edge));
struct edge *e = (struct edge *) bsearch(l, array, Narray, sizeof(struct edge), struct_cmp_by_v1);
The array is small with large numbers u32 but I only look for an element according to a field of the ,struct edge, that is v1 then the comparison is only made between v1.
The key used by bsearch is, struct edge *l, where l->v1 contains the element to be searched.
The ,array of struct edge, has element v1 to find but bserach does not find it and return NULL, I do not see the error that I'm committing
Note: The array contains only 10 elements to test but it can be a very large array with 25056012 elements, even more