Hi All,
Please find the program where I'm passing key value '6' for searching in array of structures but it is returning 'The number is in the array cdng::8 grp_idx::1' as output. Can somebody please let me know what I'm doing wrong?
------------------------------------
#include <stdio.h>
struct grp_cdng
{
const int *cdng;
const int *grp_idx;
};
struct grp_cdng new_grp[] =
{
{6,0},
{7,3},
{8,1},
{9,3},
{13,2}
};
int count = sizeof(new_grp) / sizeof(struct grp_cdng);
int compare(const struct grp_cdng *one, const struct grp_cdng *two)
{
return one->cdng - two->cdng;
}
int main(void)
{
struct grp_cdng *search_me,search_key;
search_key.cdng = 6;
printf("before bsearch:\n");
search_me=bsearch(&search_key,new_grp,count,sizeof(struct grp_cdng),compare);
printf("after bsearch:\n");
if (search_me)
printf("The number is in the array cdng::%d grp_idx::%d\n",search_me->cdng,search_me->grp_idx);
else
printf("Couldn't find in array\n");
}
------------------------
Thanks,
Sharath.
Please find the program where I'm passing key value '6' for searching in array of structures but it is returning 'The number is in the array cdng::8 grp_idx::1' as output. Can somebody please let me know what I'm doing wrong?
------------------------------------
#include <stdio.h>
struct grp_cdng
{
const int *cdng;
const int *grp_idx;
};
struct grp_cdng new_grp[] =
{
{6,0},
{7,3},
{8,1},
{9,3},
{13,2}
};
int count = sizeof(new_grp) / sizeof(struct grp_cdng);
int compare(const struct grp_cdng *one, const struct grp_cdng *two)
{
return one->cdng - two->cdng;
}
int main(void)
{
struct grp_cdng *search_me,search_key;
search_key.cdng = 6;
printf("before bsearch:\n");
search_me=bsearch(&search_key,new_grp,count,sizeof(struct grp_cdng),compare);
printf("after bsearch:\n");
if (search_me)
printf("The number is in the array cdng::%d grp_idx::%d\n",search_me->cdng,search_me->grp_idx);
else
printf("Couldn't find in array\n");
}
------------------------
Thanks,
Sharath.