On Thu, Nov 07, 2013 at 04:35:46PM -0500, Ted Unangst wrote: > On Thu, Nov 07, 2013 at 17:48, Otto Moerbeek wrote: > >> > - for (t = toskeywords; t->keyword != NULL; t++) { > >> > - if (strcmp(s, t->keyword) == 0) { > >> > - *val = t->val; > >> > - return (1); > >> > - } > >> > - } > >> > + t = bsearch(s, toskeywords, nitems(toskeywords), sizeof(struct > >> > toskeywords), (int (*)(const void *, const void *))strcmp); > >> > >> I don't like the way this is abusing types. In fact, I don't think this > >> even works. Did you test it? A pointer to a struct toskeyword will not > >> have the same value as the keyword member. > > > > The first field of a struct has the same address as the the struct > > itself. Still I consider this bad form and overkill. > > This is true, but strcmp expects the value of t->keyword, not its > address. Have I read the code wrong?
Right, it is wrong indeed. It could have been ok if keyward was an array. -Otto