c - bsearch() doesn't find my item -


could tell me why bsearch() in following code not find item "getwidth" in list? tried several compilers , works none of them must bug in code. however, don't see what's wrong there. callback passed bsearch() returns != 0 still, called 5 times , null returned bsearch() although didn't iterate on items. why that?

here code:

#include <stdio.h> #include <stdlib.h> #include <ctype.h>  typedef struct wxluabindmethod {     const char *name;     int method_type;     void *wxluacfuncs;     int wxluacfuncs_n;     void *basemethod; } wxluabindmethod;  #define wxluamethod_constructor 0x0001 #define wxluamethod_method 0x0002 #define wxluamethod_delete 0x2000  wxluabindmethod wxsize_methods[] = {     { "decby", wxluamethod_method, null, 1, null},     { "decto", wxluamethod_method, null, 1, null},     { "getheight", wxluamethod_method, null, 1, null},     { "getwidth", wxluamethod_method, null, 1, null},     { "incby", wxluamethod_method, null, 1, null},     { "incto", wxluamethod_method, null, 1, null},     { "isfullyspecified", wxluamethod_method, null, 1, null},     { "scale", wxluamethod_method, null, 1, null},     { "set", wxluamethod_method, null, 1, null},     { "setdefaults", wxluamethod_method, null, 1, null},     { "setheight", wxluamethod_method, null, 1, null},     { "setwidth", wxluamethod_method, null, 1, null},     { "delete", wxluamethod_method|wxluamethod_delete, null, 1, null},     { "op_add", wxluamethod_method, null, 1, null},     { "op_div", wxluamethod_method, null, 1, null},     { "op_eq", wxluamethod_method, null, 1, null},     { "op_iadd", wxluamethod_method, null, 1, null},     { "op_idiv", wxluamethod_method, null, 1, null},     { "op_imul", wxluamethod_method, null, 1, null},     { "op_isub", wxluamethod_method, null, 1, null},     { "op_mul", wxluamethod_method, null, 1, null},     { "op_ne", wxluamethod_method, null, 1, null},     { "op_set", wxluamethod_method, null, 1, null},     { "op_sub", wxluamethod_method, null, 1, null},     { "wxsize", wxluamethod_constructor, null, 1, null},     { 0, 0, 0, 0 }, };   int wxluabindmethod_comparebynamefnget(const void *p1, const void *p2) {     int v = strcasecmp(((const wxluabindmethod*)p1)->name, ((const wxluabindmethod*)p2)->name);      printf("cmp: %s = %s? --> %d\n", ((const wxluabindmethod*)p1)->name, ((const wxluabindmethod*)p2)->name, v);      return v; }  int main(int argc, char *argv[]) {     wxluabindmethod methoditem = { "getwidth", 10, 0, 0, 0 };     wxluabindmethod *wxlmethod;      wxlmethod = (wxluabindmethod *)bsearch(&methoditem, wxsize_methods, 25, sizeof(wxluabindmethod), wxluabindmethod_comparebynamefnget);      printf("result: %p\n", wxlmethod);      return 0;                                                        }  

and here output program generates:

cmp: getwidth = delete? --> 3 cmp: getwidth = op_isub? --> -8 cmp: getwidth = op_iadd? --> -8 cmp: getwidth = op_div? --> -8 cmp: getwidth = op_add? --> -8 result: 0x0 

i don't see why doesn't work although it's few lines. can shed light onto strange behaviour? thanks!

because wxsize_methods not sorted case-insensitive alphabetic order. should sort wxsize_methods strcasecmp before binary search.


Comments

Popular posts from this blog

image - ClassNotFoundException when add a prebuilt apk into system.img in android -

I need to import mysql 5.1 to 5.5? -

Java, Hibernate, MySQL - store UTC date-time -