c - Trouble using bsearch with an array of strings -


i getting confusing behaviour trying use c builtin bsearch on array of strings in c. here code. know can use builtin strcmp searching arrays of strings, included mystrcmp debugging purposes because didn't know why wasn't working.

const char *statenames[] = {"alabama", "alaska", "arizona", "arkansas", "california", "colorado", "connecticut", "delaware", "florida", "georgia", "hawaii", "idaho", "illinois", "indiana", "iowa", "kansas", "kentucky", "louisiana", "maine", "maryland", "massachusetts", "michigan", "minnesota", "mississippi", "missouri", "montana", "nebraska", "nevada", "new hampshire", "new jersey", "new mexico", "new york", "north carolina", "north dakota", "ohio", "oklahoma", "oregon", "pennsylvania", "rhode island", "south carolina", "south dakota", "tennessee", "texas", "utah", "vermont", "virginia", "washington", "washington dc", "west virginia", "wisconsin", "wyoming"};  int mystrcmp(const void *s1, const void *s2) {   printf("mystrcmp: s1(%p): %s, s2(%p): %s\n", s1, (char *)s1, s2, (char *)s2);   return strcmp(s1, s2); }  int determinestate(char *state) {   printf("state: %s\n", state);   for(int = 0; < 51; i++)      printf("statenames[%i](%p): %s\n", i, &(statenames[i]), statenames[i]);    char *found = (char *) bsearch(state, statenames, 51, sizeof(char *), mystrcmp );    if(found == null)     return -1;    return 0; } 

and here of output when function called alabama.

statenames[0](0x618440): alabama statenames[1](0x618448): alaska statenames[2](0x618450): arizona ... statenames[24](0x618500): missouri statenames[25](0x618508): montana statenames[26](0x618510): nebraska statenames[27](0x618518): nevada statenames[28](0x618520): new hampshire statenames[29](0x618528): new jersey statenames[30](0x618530): new mexico statenames[31](0x618538): new york statenames[32](0x618540): north carolina statenames[33](0x618548): north dakota statenames[34](0x618550): ohio statenames[35](0x618558): oklahoma statenames[36](0x618560): oregon statenames[37](0x618568): pennsylvania statenames[38](0x618570): rhode island statenames[39](0x618578): south carolina statenames[40](0x618580): south dakota statenames[41](0x618588): tennessee statenames[42](0x618590): texas statenames[43](0x618598): utah statenames[44](0x6185a0): vermont statenames[45](0x6185a8): virginia statenames[46](0x6185b0): washington statenames[47](0x6185b8): washington dc statenames[48](0x6185c0): west virginia statenames[49](0x6185c8): wisconsin statenames[50](0x6185d0): wyoming mystrcmp: s1(0x415430): alabama, s2(0x618508):                                                 ua mystrcmp: s1(0x415430): alabama, s2(0x618570): mystrcmp: s1(0x415430): alabama, s2(0x618540): pua mystrcmp: s1(0x415430): alabama, s2(0x618528): 1ua mystrcmp: s1(0x415430): alabama, s2(0x618538): gua mystrcmp: s1(0x415430): alabama, s2(0x618530): <ua 

as can see, locations visited bsearch in course of search should have valid strings (as checked before calling bsearch), output if try print char * @ location garbage. can see mistake? incidentally same bad behaviour (but don't follow closely obviously) when call bsearch final parameter set to:

(int(*)(const void*, const void*))strcmp 

thanks!

since using array of const char *, bsearch() pass comparison function pointer elements. in other words, receive const char * const * in second argument.

int mystrcmp(const void *s1, const void *s2) {   const char *key = s1;   const char * const *arg = s2;   printf("mystrcmp: s1(%p): %s, s2(%p): %s\n", s1, key, s2, *arg);   return strcmp(key, *arg); } 

Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -