I've been working in my activity that involves searching an array and i'm currently using bsearch for that matter. The problem is that whenever that element is smaller to its previous element, the bsearch could not search it. And what's more is that this problems starts to occur when the element I am searching is in an index that is greater than or equal to 3. I'm coding in C btw. Please I desperately need your help guys and it would be highly appreciated thanks. Here's my code:
#include<stdio.h>
#include<stdlib.h>
#define MAX 10
typedef struct{
int elements[MAX];
int count;
}SET;
int cmpfunc(const void * a, const void * b) {
return (*(int*)a > *(int*)b) - (*(int*)a < *(int*)b);
}
void print(SET *s1, SET *s2){
int key = 2;
int *p;
p = bsearch(&key,&s1->elements,s1->count,sizeof(int),cmpfunc);
printf("%p",p,sizeof(s1->elements));
return;
}
int main () {
SET s1 = {{1,5,4,2,6,3},6};
SET s2 = {{1,29,3,5},4};
print(&s1,&s2);
return(0);
}